math.random bug?

Hi;

Just starting to delve into Corona and Lua, and I’m having a minor problem with the simple math.random function. I have a ship that’s supposed to start each new screen at a random position and rotation, and while it works perfectly in the simulator, on my first-gen iPod Touch it always appears in the same position with the same rotation. Is this a bug, or do I need to generate a random seed for each new screen?

The math.random code is pretty simple:

 FedShip.x = math.random(32, 250)  
 FedShip.y = math.random(32, 450)  
 FedShip:rotate(45 \* (math.random (0, 7)))  

Any suggestions would be appreciated.

– Walt Sterdan [import]uid: 4329 topic_id: 358 reply_id: 300358[/import]

Walt,

have you tried to use “math.randomseed(x)” in order to set the seed of the built-in pseudo-random generator? You may use the current date/time in order to get a “random” “x”.

Kind rgeards,

Andreas Rozek [import]uid: 4331 topic_id: 358 reply_id: 613[/import]

Hi Andreas;

No, I haven’t tried it yet; I was curious if I had to, or if there was a bug in the math.random function that might be fixed in the next update.

I will give it a try rather than waiting, though, thanks.

– Walt Sterdan [import]uid: 4329 topic_id: 358 reply_id: 614[/import]

math.random will result in a “random” number every time you do a command-R in the simulator, but if you actually relaunch the simulator, the number will always be the same. This has to do with the seed on the system being the same. Doing something like math.randomseed(os.time()) or randomseed(os.clock()) will yield a more random number. [import]uid: 5 topic_id: 358 reply_id: 617[/import]

Thanks for the clarification, it’s appreciated.

Just wanted to add that I’m really enjoying Corona, great job!

–Walt Sterdan [import]uid: 4329 topic_id: 358 reply_id: 626[/import]

I am having the same problem with my random not being random. What is wrong with my code?

math.randomseed(os.clock()); canToPop=math.random(numOfCans); [import]uid: 5594 topic_id: 358 reply_id: 1219[/import]

use os.time() instead of os.clock()

math.randomseed( os.time() )

carlos [import]uid: 24 topic_id: 358 reply_id: 1220[/import]

Same problem… [import]uid: 5594 topic_id: 358 reply_id: 1221[/import]

what is value for numOfCans?

Here is the semantics for math.random

math.random(fooValue) generates integer numbers between 1 and upper. (numOfCans?)
math.random(foolowerValue, fooupperValue) generates integer numbers between lower and upper.
[import]uid: 24 topic_id: 358 reply_id: 1222[/import]

numOfCans is a tonumber(event.id) from a button press. tpye(numOfCans) is number when tested.

The result is supposed to be a number between 1 and numOfCans.

The result is in the correct range, its just always the same pattern of numbers. It seems the randomseed is not working. [import]uid: 5594 topic_id: 358 reply_id: 1223[/import]

send me some code…

info@anscamobile.com

c [import]uid: 24 topic_id: 358 reply_id: 1226[/import]

Email Sent. [import]uid: 5594 topic_id: 358 reply_id: 1230[/import]

Got it, replied.

math.randomseed( os.time() ) – needed to be placed at the top of the file.

The issue was randomseed was called everytime right before math.random was being called, thus not seeding the right random number.

c [import]uid: 24 topic_id: 358 reply_id: 1231[/import]

Hmm… this bug is back… Fixed another piece of code that was masking this problem (buttons were being called multiple times on single button presses)

Can I send you the code again? [import]uid: 5594 topic_id: 358 reply_id: 1267[/import]

yes, please send code to info@anscamobile.com, or suppor@anscamobile.com

carlos [import]uid: 24 topic_id: 358 reply_id: 1272[/import]

Still waiting for a response… [import]uid: 5594 topic_id: 358 reply_id: 1308[/import]

This may sound crazy, but what happens if you call a few random numbers (and, say, print them to the terminal). Is the SEQUENCE the same each time?

I ask because I think I’ve hit cases where the first “random” number tended to come out the same each time, even with a random seed, but then later numbers in the series would have more random-looking values. So I just discarded the first number and used the second one.

However, if you’re getting entire identical sequences, then something else is wrong. [import]uid: 3007 topic_id: 358 reply_id: 1334[/import]

Since I’ve seen a few examples (not here, but in other places) where folks set the random seed multiple times, I wanted to point out you only want to set it *once*. Setting it more than once just screws with the algorithm. Typically, one would set the seed with a call to “time(0)” as that is, on most systems, the number of seconds since the “epoch” (when unix first came to life). Now, I could be wrong, but this is, in my experience, still a very standard library call and in lua it’s available with “os.time()”. It does depend on the hardware you’re on, but the iPhone definitely supports this convention and I would suggest that any platform we would be using Corona on will as well. Using os.clock() is not a reliable thing since it’s very conceivable you’ll get the same number (or very close to the same number) each time you run your app since it’s the number of CPU seconds used since starting your app… whereas “os.time()” will always (unless called again within the same second) be different. And since you only seed *once*, this should never be an issue.

The “pseudo-random” algorithm, simply put, generates a pseudo-random sequence of numbers. Calling random just grabs the next one in the sequence. Setting seed more than once will just reset the sequence and actually give you less randomness.

What a lot of folks also don’t know (and why the OP considered the behavior to be a bug) is the same seed will generate the *same* sequence of numbers! This is a very good thing! :slight_smile: Say you randomly generate a dungeon or a set of obstacles in your game. Well, you can now “save” that level simply by storing your random seed you used to generate it… instead of saving the entire level data. To recreate it, just set your seed and run through the same algorithm. Of course, this will only work if you don’t change how you generate your level, but for certain things it’s quite nice.

At any rate, set the seed once, then call random multiple times.

In the latter part of this thread the indication was it generated the same result. This is most likely exactly what evank was saying: You’re likely just encountering the same starting numbers. If you are taking random(1,5) you have a very small sample set and will end up with similar sequences (or at least the feel of similar sequences) no matter what was being used to generate “random” numbers since you literally only have 5 numbers to choose from.

Hope this helps a little bit in understanding how random works. [import]uid: 5659 topic_id: 358 reply_id: 1417[/import]

I am well aware of how random numbers and seeds work. There was a bug in my code that was changing the seed each time, but that still should not have given me the same “random” number every time in the original version I had thrown together for testing. Even when the random seed was put in the right spot (out of the loop), the first random number was always the same, and as my game loop never needed a second random, I never got anything but the first number, which was always the same. After discussing with the developers, it seems that a simple discard of the first random number will allow the following randoms to be correct.

I still consider this a bug, but one I can work around.

(I am not some beginner programmer, I am just new to Corona and LUA. I have my BS and MS in Computer Science and work as a developer for business applications) [import]uid: 5594 topic_id: 358 reply_id: 1438[/import]

No need to get defensive, more than just you read these bug reports and there was enough conjecture here (as well as in other places) that I wanted to post about not seeding multiple times… just to be sure. I don’t know you, I couldn’t tell your level of expertise.

I, also, recently ran into the random bug you point out (it is indeed a bug) and it was very simple to reproduce.

So, devs, here you go: main.lua ::

[code]
– without seeding, you get a different sequence and different starting number every run
print(“No Seed Set”);
for i = 1, 10 do
print(math.random(1, 100));
end;

[code]
– as soon as you seed, you get the same starting number
_seed = os.time();
print("Seed: "…_seed);
math.randomseed(_seed);
for i = 1, 10 do
print(math.random(1, 100));
end;

[code]
– and again to demonstrate the same problem each re-seed
_seed = _seed + 1;
print("Seed: "…_seed);
math.randomseed(_seed);
for i = 1, 10 do
print(math.random(1, 100));
end;

-- finally, the workaround  
\_seed = \_seed + 1;  
print("Seed: "..\_seed);  
math.randomseed(\_seed);  
math.random(); -- \<\<\<\<\< this is the work around...ick.  
for i = 1, 10 do  
 print(math.random(1, 100));  
end;  

If you seed, this shows the first number in each sequence to be the same.
Specifically, if you re-seed you again get the same number for the first in the sequence.
And the last run shows the workaround.

I did some further testing and found some odd things:

  1. Over a period of about 10 minutes, the first number went from 25 to 26 to 27. I was not able to reproduce what this coincided with.
  2. It appears to only be related to actually calling math.randomseed and then running through a sequence. When I do not seed, I get a different sequence (and first number) ever time I run the test. So it appears this bug is directly in connection with performing a seed.

I completely agree with menace960 – while there is a workaround, this is definitely a bug and one that comes up each time you seed. This should definitely be flagged as a priority bug since anyone using randomseed will get bitten.

Scott
[import]uid: 5659 topic_id: 358 reply_id: 1439[/import]