Pseudo-random numbers/Xorshift star: Difference between revisions

Line 383:
0: 20103 1: 19922 2: 19937 3: 20031 4: 20007
</pre>
 
=={{header|Lua}}==
{{trans|C}}
<lang lua>function display(t)
io.write('{')
local first = true
for k,v in pairs(t) do
if first then
first = false
else
io.write(',')
end
io.write(k..'=')
local tv = type(v)
if tv == 'number' then
io.write(v)
elseif tv == 'table' then
print('a table')
elseif tv == 'function' then
print(v)
else
print('something else: '..tv)
end
end
io.write("}\n")
end
 
function create()
local g = {
magic = 0x2545F4914F6CDD1D,
state = 0,
seed = function(self, num)
self.state = num
end,
next_int = function(self)
local x = self.state
x = x ~ (x >> 12)
x = x ~ (x << 25)
x = x ~ (x >> 27)
self.state = x
local answer = (x * self.magic) >> 32
return answer
end,
next_float = function(self)
return self:next_int() / (1 << 32)
end
}
return g
end
 
local g = create()
g:seed(1234567)
print(g:next_int())
print(g:next_int())
print(g:next_int())
print(g:next_int())
print(g:next_int())
print()
 
local counts = {[0]=0, [1]=0, [2]=0, [3]=0, [4]=0}
g:seed(987654321)
for i=1,100000 do
local j = math.floor(g:next_float() * 5.0)
counts[j] = counts[j] + 1
end
for i,v in pairs(counts) do
print(i..': '..v)
end</lang>
{{out}}
<pre>3540625527
2750739987
4037983143
1993361440
3809424708
 
0: 20103
1: 19922
2: 19937
3: 20031
4: 20007</pre>
 
=={{header|Phix}}==
1,452

edits