This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
i_am_th0r's profile picture

RNG or Random Number Generator

Started by i_am_th0r, 11 December 2012 - 11:40 AM
i_am_th0r #1
Posted 11 December 2012 - 12:40 PM
Is there a way to implant a RNG into a program using lua?ive looked but i cant figure out a way.Thank you in advance.This is going to the game im programming in CC.
dissy #2
Posted 11 December 2012 - 01:43 PM
That would be the math.random() command.
With no arguments it returns a floating point value between 0 and 1
Or you can call it as math.random(10) to generate a random integer number between 1 and 10

Before using it, you will want to set the RNG seed to something as unpredictable as possible.
To do so use math.randomseed(int)
Normally in CC I do that with math.randomseed(os.time())
PixelToast #3
Posted 11 December 2012 - 01:53 PM
usage:
math.random([m[,n]])
When called without arguments, returns a uniform pseudo-random real number in the range [0,1). When called with an integer number m, math.random returns a uniform pseudo-random integer in the range [1, m]. When called with two integer numbers m and n, math.random returns a uniform pseudo-random integer in the range [m, n].
Dlcruz129 #4
Posted 11 December 2012 - 01:55 PM
usage:
math.random([m[,n]])
When called without arguments, returns a uniform pseudo-random real number in the range [0,1). When called with an integer number m, math.random returns a uniform pseudo-random integer in the range [1, m]. When called with two integer numbers m and n, math.random returns a uniform pseudo-random integer in the range [m, n].

Off-topic: Umm… Pixel? Your location says "cruor." One quick question: WTF?!
n1ghtk1ng #5
Posted 11 December 2012 - 03:09 PM
Like this?

local t = {1, 2, 3, 4, 5}
a = math.random(1, #t)
print(a)
Bubba #6
Posted 11 December 2012 - 03:18 PM
Like this?

local t = {1, 2, 3, 4, 5}
a = math.random(1, #t)
print(a)

Yup that would work if you're trying to get a random index from the table.
PixelToast #7
Posted 11 December 2012 - 03:21 PM
Like this?

local t = {1, 2, 3, 4, 5}
a = math.random(1, #t)
print(a)
sould be

local t = {1, 2, 3, 4, 5}
local a = t[math.random(1, #t)]
print(a)
if you want to get a random thing from the table