31 posts
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?
1281 posts
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
31 posts
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
995 posts
Location
Canada
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.
1281 posts
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.
31 posts
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
1281 posts
Posted 01 February 2014 - 06:09 PM
I don't… what? That's exactly what i posted, just without the function definition.
31 posts
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
7508 posts
Location
Australia
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
1281 posts
Posted 01 February 2014 - 08:14 PM
Really didn't think that would be nessacary on a 2 line function…
7508 posts
Location
Australia
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.
1281 posts
Posted 01 February 2014 - 08:39 PM
Yeah, i guess you're right. I'll try to keep that in mind next time.
31 posts
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