I want to get a random number 1-100 and then have a response depending on what the number is. Is this possible?
I want to get a random number 1-100 and then have a response depending on what the number is. Is this possible?
Yes. I looked and didn't find anything. Instead of wasting a comment, why not just tell me what it is?
It's on the page of google's first result. Let me quote it for you
[spoiler]
math.random , math.randomseed
math.random() generates pseudo-random numbers uniformly distributed. Supplying argument alters its behaviour:
- math.random() with no arguments generates a real number between 0 and 1.
- math.random(upper) generates integer numbers between 1 and upper.
- math.random(lower, upper) generates integer numbers between lower and upper.
> = math.random()
0.0012512588885159
> = math.random()
0.56358531449324
> = math.random(100)
20
> = math.random(100)
81
> = math.random(70,80)
76
> = math.random(70,80)
75
upper and lower must be integer. In other case Lua casts upper into an integer, sometimes giving math.floor(upper) and others math.ceil(upper), with unexpected results (the same for lower). The math.randomseed() function sets a seed for the pseudo-random generator: Equal seeds produce equal sequences of numbers.
> math.randomseed(1234)
> = math.random(), math.random(), math.random()
0.12414929654836 0.0065004425183874 0.3894466994232
> math.randomseed(1234)
> = math.random(), math.random(), math.random()
0.12414929654836 0.0065004425183874 0.3894466994232
A good 'seed' is os.time(), but wait a second before calling the function to obtain another sequence! To get nice random numbers use:
math.randomseed( os.time() )
If Lua could get milliseconds from os.time() the init could be better done. Nevertheless, in some cases we need a controlled sequence, like the obtained with a known seed.
But beware! The first random number you get is not really 'randomized' (at least in Windows 2K and OS X). To get better pseudo-random number just pop some random number before using them for real:
-- Initialize the pseudo random number generator
math.randomseed( os.time() )
math.random(); math.random(); math.random()
-- done. :-)
-- This not exactly true. The first random number is as good (or bad) as the second one and the others. The goodness of the generator depends on other things. To improve somewhat the built-in generator we can use a table in the form:
-- improving the built-in pseudorandom generator
do
local oldrandom = math.random
local randomtable
math.random = function ()
if randomtable == nil then
randomtable = {}
for i = 1, 97 do
randomtable[i] = oldrandom()
end
end
local x = oldrandom()
local i = 1 + math.floor(97*x)
x, randomtable[i] = randomtable[i], x
return x
endend
[4] : Why math.random() might give weird results on OSX and FreeBSD?
...The problem seems to be that when the seeds differ very little the first value of generated by BSD rand() also differ very little. This difference is lost when Lua converts the integer returned by rand() into a real number, effectively preserving only the high bits in the result. When you call math.random(1,100) from Lua, the low-bit difference vanishes and you see the same integer result.
There is also lrandom[5] A library for generating random numbers based on the Mersenne Twister.
[/spoiler]
You obviously didn't search enough
if I remember correctly it wasCode:math.random(1,100)