lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It works exactly as I want. I used math.random, but always got the same numbers, but if I call math.randomseed(os.time()) before, I always got different numbers. Thank you very much!



----- Original Message ----- From: "Eike Decker" <zet23t@googlemail.com>
To: "Lua list" <lua@bazar2.conectiva.com.br>
Sent: Saturday, January 16, 2010 8:28 PM
Subject: Re: Getting randomic integers


The randomseed function takes one number that is being used to
initialize the random number generator - otherwise, you'll always get
the same random numbers.

For example:

math.randomseed(os.time()) -- initialize based on actual time
for i=1,50 do print(math.random(100)) end -- generate random 50 random
numbers between 1 and 100

Check the manuals for accurate description though:
http://www.lua.org/manual/5.1/manual.html#pdf-math.random

Eike

2010/1/16 Luciano de Souza <luchyanus@predialnet.com.br>:
Hi all,

I could not understand how to get a trully randomic integer. Suppose I need 50 randomic integers each time I call a function. Serching in documentation,
I found two functions in Math module: math.random and math.randomseed.

The first one is not what I want. Calling it, I got always the same 50
numbers. In Pascal, before using random functions, it's necessary to call
"randomize" which starts the randomic number generator. I don't know if it's
also necessary in Lua.

But I try to use math.randomseed. The first tesst is:

print(math.randomseed(100))

print(math.randomseed(0, 100))

Despite no errors, I dindn't get anything. So I ask: how to obtain 50
different integers each time I call the randoms functions?

Luciano de Souza