lua-users home
lua-l archive

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


These integers are not *truly* random, they are generated deterministically using some algorithm chosen because it's fast, and the quality depends on the particular generator chosen by whoever created it in your C library.

Don't use them for anything relating to cryptography. They may not even be good enough for use in operations research, it may be easy to find patterns in the stream.

Ge'


On 01/16/2010 03:05 PM, Luciano de Souza wrote:
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




--
Gé Weijers email: ge@weijers.org