math.random() Percentage

I am developing a game that needs percentage.

The game needs percentage that is quiet Accurate. (Like WELL512 Algorithm.)

Also the percentage needs to be restricted by the players level and it’s quantity have to be restricted too.

Can anyone give me a Awesome algorithm or API That does stuff like it?

↓ I tried these sources. First one is just test for easy use math.random second one is for Monte-Carlo Calculation.

(For people try setting math.randomseed( os.time() ) …  I tried it.)

function maybe(x) if 100 \* math.random() \< x then print(1) else print(0) end end

local A1, A2 = 727595, 798405 -- 5^17=D20\*A1+A2 local D20, D40 = 1048576, 1099511627776 -- 2^20, 2^40 local X1, X2 = 0, 1 function rand() local U = X2\*A2 local V = (X1\*A2 + X2\*A1) % D20 V = (V\*D20 + U) % D40 X1 = math.floor(V/D20) X2 = V - X1\*D20 return V/D40 end

math.random() with no additional parameters should return a value in the range of 0…1. That means simply multiple by 100 to get a percentage:

local percent = math.random() \* 100

If you don’t want the fractional value:

local percent = math.floor( math.random() \* 100 )

Rob

Sorry to butt in Rob, but I’m wondering if @zero_raws is looking for a random generator that is guaranteed to be evenly distributed.

The word ‘accurate’ was used, but I can’t think of a context where a percentage could be accurate or not accurate, so my guess is s/he meant even.

@zero_raw

Do you mean you want  a random number generator that if called 100 times would produce values equivalent to 1,2,3,4…100 once each?

If so, and this is starting to be repetitive because I’ve said it elsewhere, you want a shuffle bag:

http://github.com/roaminggamer/CoronaGeek/tree/master/Hangouts/Tips_and_Tricks/shufflebag

With a shuffle bag, you can ‘fill the bag’ with the distribution you want and then randomly pull values our till the bag is empty, re-fill, reshuffle repeat.  

This produces a guaranteed distribution matching your needs over any span equal to the fill size.

I’m also a bit unsure what your specific needs are, but you might try my MWC plugin as well, which is based on one of George Marsaglia’s generators.

Another pure Lua approach would be to port Bob Jenkins’s ISAAC to Lua 5.1. Dirk Laurie ported it (from C) to 5.3 some time back. By the looks of it that would mostly entail moving the operators (>>, ~, &) to their bit.* counterparts (negative shifts to lshift , positive ones to rshift or maybe arshift , ~ to bxor , & to band ). It seems to have pretty respectable properties.

math.random() with no additional parameters should return a value in the range of 0…1. That means simply multiple by 100 to get a percentage:

local percent = math.random() \* 100

If you don’t want the fractional value:

local percent = math.floor( math.random() \* 100 )

Rob

Sorry to butt in Rob, but I’m wondering if @zero_raws is looking for a random generator that is guaranteed to be evenly distributed.

The word ‘accurate’ was used, but I can’t think of a context where a percentage could be accurate or not accurate, so my guess is s/he meant even.

@zero_raw

Do you mean you want  a random number generator that if called 100 times would produce values equivalent to 1,2,3,4…100 once each?

If so, and this is starting to be repetitive because I’ve said it elsewhere, you want a shuffle bag:

http://github.com/roaminggamer/CoronaGeek/tree/master/Hangouts/Tips_and_Tricks/shufflebag

With a shuffle bag, you can ‘fill the bag’ with the distribution you want and then randomly pull values our till the bag is empty, re-fill, reshuffle repeat.  

This produces a guaranteed distribution matching your needs over any span equal to the fill size.

I’m also a bit unsure what your specific needs are, but you might try my MWC plugin as well, which is based on one of George Marsaglia’s generators.

Another pure Lua approach would be to port Bob Jenkins’s ISAAC to Lua 5.1. Dirk Laurie ported it (from C) to 5.3 some time back. By the looks of it that would mostly entail moving the operators (>>, ~, &) to their bit.* counterparts (negative shifts to lshift , positive ones to rshift or maybe arshift , ~ to bxor , & to band ). It seems to have pretty respectable properties.