math.random bug?

Added into our bug base to look into and fix

thanks

Carlos [import]uid: 24 topic_id: 358 reply_id: 1441[/import]

(Logged as bug #134) [import]uid: 3 topic_id: 358 reply_id: 2038[/import]

I’m not able to get math.random to work on the iphone4 side. My random word generator works on the Android side, and in the emulator but opening up the dev app on the iphone 4 shows the exact same results every time.
I’ve tried the math.randomseed(os.clock()); before attemting any randomization but it doesn’t help.

Is there a new bug?
[import]uid: 9046 topic_id: 358 reply_id: 15623[/import]

I just did some further testing and I can almost confirm this is a bug on compile. I took an app that we have published in the apple app store and recompiled it using the new version of Corona. On the iphone4 - math.random is no longer working as it did when I compiled the published app early December 2010.

Arg, very frustrating as I was almost finished with a new app when I spotted this bug.

Help -
[import]uid: 9046 topic_id: 358 reply_id: 15625[/import]

If you had even bothered to look through the thread you would know you need to use os.time() instead of os.clock() for your seed. [import]uid: 10835 topic_id: 358 reply_id: 15644[/import]

@IgnacioIturra - Of course I looked through the thread, I try to practice studying a problem well enough before posting blindly into the forum and filling everyones inbox with nonsense…
I tried all methods I could find to get the function to work but it’s still not working on the iphone side.
Did you try and duplicate the problem for yourself by compiling for iphone4 or are you just posting blindly without “bothering” to do the work ;)? Sorry -couldn’t resist!
I just want the bug fixed -
[import]uid: 9046 topic_id: 358 reply_id: 15645[/import]

I stopped using Corona after my initial trial expired due to this bug. Just an FYI [import]uid: 5594 topic_id: 358 reply_id: 15646[/import]

“I’ve tried the math.randomseed(os.clock()); before attemting any randomization but it doesn’t help.”

That alone tells me you haven’t. As a matter of fact I HAVE dealt with this bug during development of my game and it was fixed by following the instructions in this very thread (which you haven’t properly read if you still can’t fix it).

I’ll reiterate just for your benefit.

math.randomseed(os.time())

at the beginning of your code. Just once (make sure it’s not in a loop or called again).
then

math.random()

Once. That number will always be the same. As long as you discard it, you can use math.random again and the rest of the numbers will be “random”.

If you don’t do this, it’ll work fine on the simulator (which is why I had so much trouble finding the bug), but won’t work right on your iphone.
[import]uid: 10835 topic_id: 358 reply_id: 15647[/import]

>>Once. That number will always be the same.

And it is not a problem of Corona but with Lua. We have discussed this extensively here last year. [import]uid: 5712 topic_id: 358 reply_id: 15650[/import]

ignacio and/or mike, could you please post sample code on how you propose we approach this workaround? It could benefit the rest of us newbies :wink:

Thanks,
RD [import]uid: 7856 topic_id: 358 reply_id: 15660[/import]

But, but, but…, I just did that.

It’s really no more complicated than having this at the start of your main.lua

[lua]math.randomseed(os.time())
math.random()[/lua]

Then just use math.random() normally and it’ll work fine. [import]uid: 10835 topic_id: 358 reply_id: 15662[/import]

Post #17 from this thread gives a decent run down on how to actually seed and use random correctly:
http://developer.anscamobile.com/forum/2010/01/05/mathrandom-bug#comment-1417

Post #19 gives example code which will show the problem and the last snippet shows the work around. This is also specified in the comment right before yours.
http://developer.anscamobile.com/forum/2010/01/05/mathrandom-bug#comment-1439

For clarification:
[lua]_seed = os.time();
– finally, the workaround
– _seed = _seed + 1; commented out as it is not germane to this snippet
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;[/lua]

As others have pointed out, all right from here in this very thread. :slight_smile:

All you need to do is seed once with os.time() and then call random once and ignore it’s value. From that point on the correct random number sequence is generated.

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

Yeah, I saw (glimpse) at the posts you mentioned, but it didn’t clicked.
Sometimes devs don’t realize that new blood is coming to the forums and are learning (even experience ones).

New devs are also looking for ‘friendly forums’; some are afraid of asking question for fear of sounding “dumb” and leave.
Take a look at what menace690 posted, he left because he thought it was bug that was never going to be fixed.

Keep in mind that if potential corona users don’t buy the software because they don’t feel “right”, that’s money that Ansca is losing; and if Ansca doesn’t make money, corona could well be extinct (I’m exaggerating, but it has happened )

All this long thread could’ve been avoided (perhaps at post #17) if something as simple as the (but, but, but) was posted earlier.

Sorry for the rumble.

Thanks and cheers :wink:
RD [import]uid: 7856 topic_id: 358 reply_id: 15667[/import]

Yes, this problem has has been around for years. Anyone who’s relied on the rand() function in libc (which is what Lua relies on) has seen this problem. Here’s a note from one of the creators of Lua documenting the issue almost 4 years ago:

http://lua-users.org/lists/lua-l/2007-03/msg00564.html [import]uid: 26 topic_id: 358 reply_id: 18637[/import]

According to this post:
http://lua-users.org/wiki/MathLibraryTutorial

… OSX and FreeBSD have issues.

The solution proposed was based on the idea that os.time() returns a relatively similar number each time. For example, here are two os.time() values separated by roughly one day (A day has 86400 seconds)

1327302026 -- today  
1327388297 -- some time tomorrow  

As you can see, the numbers are strikingly close, all things considered.

The post above explains some garbal about “high bits” and "low bits’, so my instinct leads me to believe that what they are trying to say is that the seed number itself needs to be a bit more sporadic… or random, if you will.

Hence, they propose the following solution:

math.randomseed( tonumber(tostring(os.time()):reverse():sub(1,6)) )  
-- or for a larger sample, drop the "sub":  
-- math.randomseed( tonumber(tostring(os.time()):reverse()) )  
-- or to get an even larger timestamp with milliseconds:  
-- math.randomseed( tonumber(tostring(os.time() + math.round( system.getTimer()):reverse()) )  

… which essentially reverses the os.time() so that the last number is first, thereby feeding seed numbers that are vastly different:

6202037231 -- today 7928837231 -- some time tomorrow [import]uid: 616 topic_id: 358 reply_id: 81823[/import]

Thanks for that, I too have seen that random numbers aren’t quite as random as I’d like.

As an aside, I’ve been using this library for my games which require procedural generation - it will give the same set of random numbers for a given seed across all platforms.

http://developer.anscamobile.com/code/portable-seedable-random-number-generator

[import]uid: 93133 topic_id: 358 reply_id: 81854[/import]