Post Reply 
Exact Discrete pseudo-Random Sampling
04-08-2020, 08:56 PM (This post was last modified: 04-08-2020 09:22 PM by Albert Chan.)
Post: #5
RE: Exact Discrete pseudo-Random Sampling
For random number with small range (say, dice games), bit-mask is simple, and fast.
see O'Neill's benchmark, Efficiently Generating a Number in a Range

lua> mt19937 = require 'mt19937' -- Mersenne Twister PRNG
lua> rand = mt19937.new(os.clock())
lua> dice = ('%010o'):format(rand(0, 2^30-1))
lua> dice                                      -- upto 10 valid dice throws !
3746210703
lua> dice = dice:gsub('[07]', '')      -- remove invalid throws
lua> dice
346213
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Exact Discrete pseudo-Random Sampling - Albert Chan - 04-08-2020 08:56 PM



User(s) browsing this thread: 1 Guest(s)