random number generation

hi all

I’m trying to get the random number by using following code to fetch a predefined value from numerArray.

numberArray = [4,5,6,77,88,33]  
function testing ()  
 math.randomseed(os.time())  
 value = numberArray [math.random(1,8)]   
end  
timer.performWithDelay(8000, testing, 30)  

the out put of math.random is generating the same number for 3 time continuously and then its generating(every 4th time) new random number.

for 1st 3 calls its giving 5,5,5
next 3 calls = 1,1,1
next 3 calls = 8,8,8

is the diff between min and max values in the math.random should be more?
any better solution? [import]uid: 50443 topic_id: 30619 reply_id: 330619[/import]

first of all, you only need to seed the random number generator once.

Second of all, you’re asking for a range of numbers, 1 to 8, yet you only have 6 cells in your array, so 25% of the time you will be getting nil for your value.

Next, we never see where you’re outputting your value so we don’t know what you are actually outputting.
[import]uid: 19626 topic_id: 30619 reply_id: 122681[/import]

thanks for quick reply.

actually in that arry there r 8 values and prints the value which i missed above…

i have many places where this math.random will be called, so by ur explanation in start of the program i can seed once and can call math.random any where in deferent functions/ threads?? [import]uid: 50443 topic_id: 30619 reply_id: 122683[/import]

also note: os.time() is only timed to a second.

So if your using the same seed time more than once in ONE second… you will get the same number back… because your seed has not changed. (least this is my understanding)

[import]uid: 88147 topic_id: 30619 reply_id: 122687[/import]

@team1, I believe that is correct.

In most cases you want the seed to be unique for each invocation of the program. There are times however where you want to repeat a sequence of numbers, so you would use the same seed to reproduce those numbers. Many card games implement this feature where you can play a certain game number over and over. All they do is remember the seed for that game and then reseed the random number generator with a specific seed to get the cards in a certain order.

Back in the day, we were working on trying to do a massive-multi-player Sci-Fi game and we needed to store tons of information about each planet, like its size, gravity, moons, atmosphere, etc as well as all stellar bodies. Just looking at the amount of data, it was enormous, but for most of it, we could generate randomly and I would just need to store the random number seed for that system, then generate it as we needed it all the way down to the fractal terrain.

So yes, @sudheer.dontamsetty, you just need to seed it once at the program’s invocation and then just call math.random() when you need it… that is unless you’re doing something like I described above.
[import]uid: 19626 topic_id: 30619 reply_id: 122728[/import]

thanks guys, got good clarity abt seeding for random number. [import]uid: 50443 topic_id: 30619 reply_id: 122766[/import]

first of all, you only need to seed the random number generator once.

Second of all, you’re asking for a range of numbers, 1 to 8, yet you only have 6 cells in your array, so 25% of the time you will be getting nil for your value.

Next, we never see where you’re outputting your value so we don’t know what you are actually outputting.
[import]uid: 19626 topic_id: 30619 reply_id: 122681[/import]

thanks for quick reply.

actually in that arry there r 8 values and prints the value which i missed above…

i have many places where this math.random will be called, so by ur explanation in start of the program i can seed once and can call math.random any where in deferent functions/ threads?? [import]uid: 50443 topic_id: 30619 reply_id: 122683[/import]

also note: os.time() is only timed to a second.

So if your using the same seed time more than once in ONE second… you will get the same number back… because your seed has not changed. (least this is my understanding)

[import]uid: 88147 topic_id: 30619 reply_id: 122687[/import]

@team1, I believe that is correct.

In most cases you want the seed to be unique for each invocation of the program. There are times however where you want to repeat a sequence of numbers, so you would use the same seed to reproduce those numbers. Many card games implement this feature where you can play a certain game number over and over. All they do is remember the seed for that game and then reseed the random number generator with a specific seed to get the cards in a certain order.

Back in the day, we were working on trying to do a massive-multi-player Sci-Fi game and we needed to store tons of information about each planet, like its size, gravity, moons, atmosphere, etc as well as all stellar bodies. Just looking at the amount of data, it was enormous, but for most of it, we could generate randomly and I would just need to store the random number seed for that system, then generate it as we needed it all the way down to the fractal terrain.

So yes, @sudheer.dontamsetty, you just need to seed it once at the program’s invocation and then just call math.random() when you need it… that is unless you’re doing something like I described above.
[import]uid: 19626 topic_id: 30619 reply_id: 122728[/import]

thanks guys, got good clarity abt seeding for random number. [import]uid: 50443 topic_id: 30619 reply_id: 122766[/import]