This site requires JavaScript, please enable it in your browser!
Greenfoot back
darkmist255
darkmist255 wrote ...

2012/2/11

Random number always giving same output?

darkmist255 darkmist255

2012/2/11

#
---SOLVED--- I'm using x and y multipliers to make this actor's movement ridiculously random, but I'm getting strange results. This is the part of the code I am having problems with. It is executed every act cycle. By the way, all variables are doubles, not ints.
            xMultiplier = (Greenfoot.getRandomNumber(1000)/1000) + 1;
            yMultiplier = (Greenfoot.getRandomNumber(1000)/1000) + 1;
            
            
            if(Greenfoot.getRandomNumber(3) <= 3)
            {
                xMultiplier = xMultiplier * (-1);
            }
            if(Greenfoot.getRandomNumber(3) <= 3)
            {
                yMultiplier = yMultiplier * (-1);
            }
            
            System.out.println(xMultiplier + "," + yMultiplier); //For debugging purposes
I would expect the output to be between 1.0 and 2.0, or -2.0 and -1.0. Instead it's outputting: -1.0,-1.0 -1.0,-1.0 -1.0,-1.0 -1.0,-1.0 -1.0,-1.0 -1.0,-1.0 -1.0,-1.0 -1.0,-1.0 -1.0,-1.0 -1.0,-1.0 -1.0,-1.0 -1.0,-1.0 -1.0,-1.0 -1.0,-1.0 -1.0,-1.0 -1.0,-1.0 -1.0,-1.0 -1.0,-1.0 -1.0,-1.0 -1.0,-1.0 -1.0,-1.0 -1.0,-1.0 -1.0,-1.0 You get the idea. There appears to be no randomness happening? What could be wrong?
darkmist255 darkmist255

2012/2/11

#
Erm... Never mind... I just realized the redundancy in "if(Greenfoot.getRandomNumber(3) <= 3) " :D. Changed it to "if(Greenfoot.getRandomNumber(3) <= 1) " and all is well.
darkmist255 darkmist255

2012/2/11

#
No! Not solved. I'm still having troubles with "(Greenfoot.getRandomNumber(1000)/1000) + 1;". The results are now random for being positive or negative: -1.0,-1.0 1.0,1.0 1.0,-1.0 -1.0,-1.0 -1.0,-1.0 -1.0,-1.0 -1.0,1.0 1.0,-1.0 -1.0,-1.0 But they're still all 1.0? Strange.
iau iau

2012/2/11

#
getRandomNumber(1000) returns an integer between 0 and 999 (inclusive). You then divide that by 1000 using integer division, which (effectively) throws away the fractional part. So 1/1000 is 0, 500/1000 is 0 and 999/1000 is 0. You then add 1, so xMultiplier is always 1. You then change the sign of xMultiplier with probability 2/3. From your code, I don't quite know what values you want xMultiplier to be able to take.
kiarocks kiarocks

2012/2/11

#
Right.
danpost danpost

2012/2/11

#
Try this!
            xMult = ((0f + Greenfoot.getRandomNumber(1000)) / 1000) + 1;
            yMult = ((0f + Greenfoot.getRandomNumber(1000)) / 1000) + 1;
            int signs = Greenfoot.getRandomNumber(4);
            xMult = xMult * ((signs % 2 == 0) ? -1 : 1);
            yMult = yMult * (((signs - (signs % 2)) / 2 == 0) ? -1 : 1);
Well, this is a 50/50 chance for each of x or y to benegative. Change 4 to 9 and all 2 to 3.
danpost danpost

2012/2/11

#
Adding 0f to the initial random(1000) sets it to a float value. The signs integer does double duty for your negation: 0 x = - y = - 1 x = + y = - 2 x = + y = - 3 x = - y = + 4 x = + y = + 5 x = + y = + 6 x = - y = + 7 x = + y = + 8 x = + y = + 1 in 3 that x = - 1 in 3 that y = - 1 in 9 that both are - like a glove!
darkmist255 darkmist255

2012/2/11

#
Ah! Adding the 0f is what I was looking for! I assumed that dividing the int within a double would convert it to a double (float in the end in this case :D). Now my output is much more natural. -1.2990000247955322,-1.524999976158142 1.13100004196167,-1.9089999198913574 1.5109999179840088,1.5269999504089355 1.8029999732971191,1.7230000495910645 1.0019999742507935,1.3109999895095825 -1.4049999713897705,-1.8629999160766602 -1.4509999752044678,1.031999945640564 1.690999984741211,1.3170000314712524 1.0410000085830688,-1.5980000495910645 1.812000036239624,-1.6370000839233398 1.343999981880188,1.2400000095367432 -1.1030000448226929,1.1540000438690186 You get the idea. For the signs, I found this works well enough for me.
if(Greenfoot.getRandomNumber(100) <= 49)
                {
                    xMultiplier = xMultiplier * (-1);
                    xMultiplierAdjusted = true;
                }

//same thing for y, just with y of course
You need to login to post a reply.