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

Random number generator

Started by andyoc, 01 February 2014 - 10:56 AM
andyoc #1
Posted 01 February 2014 - 11:56 AM
so i know of math.random but my question is can i generate a random number from my own numbers instead of it just generating a number between 2..

let me explain clearer



math.random(1,10)
this generates a random number between 1 and 10 but lets say i dont want it to choose 2,6 and 8 and only want randomisation between the remainders how would i go about that?
CometWolf #2
Posted 01 February 2014 - 01:15 PM
Index the numbers you want in a numerical table and generate a random number based on the size of the table.

function random(...)
  local tNum = {...}
  return tNum[math.random(1,#tNum)]
end
Why do you need to skip certain numbers though?
Edited on 01 February 2014 - 12:40 PM
andyoc #3
Posted 01 February 2014 - 03:50 PM
that would only give me a random number of the tNum length though would it not?

if i stored 1,3 and 8 in tNum and ran math.random(1,#tNum)

that would give me a random number between 1 and 3 ?? where i want random number of 1,3 and 8

and just for knowledge
Edited on 01 February 2014 - 02:51 PM
awsmazinggenius #4
Posted 01 February 2014 - 04:38 PM
I doubt it. The length operator/function doesn't really work on tables with holes, so I think it returns 0 (or maybe nil). Just generate between 1-3 and do something like

if num == 2 then
  num = 8
end
to make the number 8 if it is not 1 or 3.
CometWolf #5
Posted 01 February 2014 - 04:53 PM
Neither of you are getting it… You index the table with keyes from 1 to however many numbers you want. This leaves no holes. Then you use math.random to randomly pick a table index, which contains one of the numbers you chose. If you use the function i posted, and give it 1,5,8,10 it will return a randomly choosen number of the 4.
andyoc #6
Posted 01 February 2014 - 05:13 PM
ok this is how i got it to work


local tNum = {2,4,5,7,9}
rNum = math.random(1,#tNum)
print (tNum[rNum])

thanks again Comet Wolf but i could not get it to work
CometWolf #7
Posted 01 February 2014 - 06:09 PM
I don't… what? That's exactly what i posted, just without the function definition.
andyoc #8
Posted 01 February 2014 - 07:21 PM
I don't… what? That's exactly what i posted, just without the function definition.

OK sorry for the confusion it works now anyway thanks for all the help
theoriginalbit #9
Posted 01 February 2014 - 07:29 PM
I don't… what? That's exactly what i posted, just without the function definition.
maybe you should have shown a usage example
CometWolf #10
Posted 01 February 2014 - 08:14 PM
Really didn't think that would be nessacary on a 2 line function…
theoriginalbit #11
Posted 01 February 2014 - 08:33 PM
Really didn't think that would be nessacary on a 2 line function…
not everyone understands code the way we do, especially when it comes to vararg functions; also it doesn't take much to add something like this to your post just for extra clarity.

use the above function like this


random(2, 3, 8)

to have it randomly pick between the numbers 2, 3, and 8.
CometWolf #12
Posted 01 February 2014 - 08:39 PM
Yeah, i guess you're right. I'll try to keep that in mind next time.
andyoc #13
Posted 01 February 2014 - 08:46 PM
ahhh i see well the way i used it was better suited to my needs but i will take not of that for future reference thanks again