About randomseed

I tested randomseed like the following :

math.randomseed(50)

for i=1,10 do
display.newText(math.random(10),50,50+i*20,nil,18)
end

On the simulator I see always the same 10 values but on my mobile, they’re always different.
So,for what is randomseed? [import]uid: 107239 topic_id: 33108 reply_id: 333108[/import]

Try math.randomseed(os.time()) – there’s an explanation in several threads if you search, the idea is to use a value that changes, hence why people often use time. [import]uid: 52491 topic_id: 33108 reply_id: 131666[/import]

Thanks but in fact it’s all the opposite I want.
I wanted to get each time the same random values when the app starts… If you put a constant value in the seed, like 50 in my example, I get always the same random values but only in the simulator; it doesn’t work on mobiles. [import]uid: 107239 topic_id: 33108 reply_id: 131709[/import]

Sounds like a bug. Have you tried just not using a Random Seed if you want the answers to be static? [import]uid: 62706 topic_id: 33108 reply_id: 131711[/import]

If I remember, without randomseed, they never static (I just tried under the simulator and it’s the case) [import]uid: 107239 topic_id: 33108 reply_id: 131714[/import]

I believe they are static on the device. Early on my development of my first app, I couldn’t understand why the simulator was random everytime, but the device was always the same, thats how I found out about randomSeed :slight_smile: [import]uid: 62706 topic_id: 33108 reply_id: 131715[/import]

I just tried with build #967 on mobile and tablet without randomseed, values are always different too. There’s a bug with it. [import]uid: 107239 topic_id: 33108 reply_id: 131719[/import]

Hmm… I’d try on the latest daily build (971 I think) and if still an issue, file a bug report. [import]uid: 62706 topic_id: 33108 reply_id: 131721[/import]

It’s not really a bug - the random number generator will generate a different set of numbers depending on the type of device it is run on, as it uses the native random number functions of each device.

This library should do the trick:

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

[import]uid: 93133 topic_id: 33108 reply_id: 131744[/import]

Try math.randomseed(os.time()) – there’s an explanation in several threads if you search, the idea is to use a value that changes, hence why people often use time. [import]uid: 52491 topic_id: 33108 reply_id: 131666[/import]

“depending on the type of device it is run” ok but why it generates ALWAYS different number ON THE SAME device when using RANDOMSEED as I asked on my first question !? [import]uid: 107239 topic_id: 33108 reply_id: 131752[/import]

I guess that is broken then, in the meantime while you wait for it to be fixed (could be anywhere between a day and several ice ages with Corona) use the library I posted instead of the built in function. [import]uid: 93133 topic_id: 33108 reply_id: 131753[/import]

That’s right :slight_smile: thanks for the link, it should save me for now! [import]uid: 107239 topic_id: 33108 reply_id: 131761[/import]

Just a quick reference to what is going on.

If you do not seed the random number generator, math.random will grab the content from it’s seed variable and use it.

If you run your app in the simulator, that seed variable will likely contain whatever data last occupied that memory location and you get the illusion that you’re getting different numbers. When you do this on a device, there is a very good likelyhood that the contents of memory will be zeroed out and thus math.random gets seeded with 0 each time, producing the same sequence of random numbers.

I can’t confirm this, but it’s a reasonable concept.

Therefore if you want to make sure you get different numbers every time, you need to do what Peach said above and use

math.randomseed(os.time())

However, if you want the same sequence of numbers over and over, you need to pass a specific integer value to math.randomseed().

If that does not produce the same sequence over and over, it’s likely a bug. [import]uid: 19626 topic_id: 33108 reply_id: 131767[/import]

Thanks but in fact it’s all the opposite I want.
I wanted to get each time the same random values when the app starts… If you put a constant value in the seed, like 50 in my example, I get always the same random values but only in the simulator; it doesn’t work on mobiles. [import]uid: 107239 topic_id: 33108 reply_id: 131709[/import]

Sounds like a bug. Have you tried just not using a Random Seed if you want the answers to be static? [import]uid: 62706 topic_id: 33108 reply_id: 131711[/import]

If I remember, without randomseed, they never static (I just tried under the simulator and it’s the case) [import]uid: 107239 topic_id: 33108 reply_id: 131714[/import]

I believe they are static on the device. Early on my development of my first app, I couldn’t understand why the simulator was random everytime, but the device was always the same, thats how I found out about randomSeed :slight_smile: [import]uid: 62706 topic_id: 33108 reply_id: 131715[/import]

I just tried with build #967 on mobile and tablet without randomseed, values are always different too. There’s a bug with it. [import]uid: 107239 topic_id: 33108 reply_id: 131719[/import]

Hmm… I’d try on the latest daily build (971 I think) and if still an issue, file a bug report. [import]uid: 62706 topic_id: 33108 reply_id: 131721[/import]