Re: How to go about daily random numbers
Posted: Sat Jan 08, 2022 10:29 pm
Wait, so I should use 13 instead of 16
Code: Select all
local d = os.date("!*t") -- utc date
local rng = love.math.newRandomGenerator(d.yday, d.year)
-- call rng for each random number when building a level:
local num = rng:random(1, 1000)
Why try to randomize your seed? It's love, so people can read all your code, right? Or change their system clock.
If you put your date number into a random number generator like grump's second suggestion (or mine), then it won't generate similar maps. A good random number generator should create different sequences of numbers for different seeds. You could multiply yday by 100 to ensure very different seeds to the rng each day, but I found my levels very different each day.Gunroar:Cannon() wrote: ↑Sat Jan 08, 2022 6:51 pm Hmmm... I guess I'll use the first method then. Just thought they might make similar maps ... for ... some reason
If building a level, I'd expect you need to make many random decisions. To generate multiple random numbers with the sha version without producing the same sequence at a later date, you'd have to concatenate a sequence number too as well as pad the numbers with 0 like grump said.
Future levels, too. Is that really a problem? Or could you just not care? I can assure you: you will regret to have opened that can of worms.Gunroar:Cannon() wrote: ↑Sun Jan 09, 2022 9:53 am Though the problem of people being able change their system clock to get old levels
Yes, inside hash:sub() only; the other 16 stays. It will then produce a number between 0 and 0xFFFFFFFFFFFFF (that's 13 F's) and then you can get a number in a certain range by using modulo (%). If you use 16, using modulo will produce zeros in the lowest bits > 99.9% of the time.
Code: Select all
local rand = tonumber(hash:sub(1, 16), 16)
Code: Select all
local rand = tonumber(hash:sub(1, 13), 16)
Yes it's slow, but you don't need to set up something as complex as a sponge function. You can hash a counter.grump wrote: ↑Sun Jan 09, 2022 1:11 pm It makes no sense to use the convoluted hash method to generate a number just because the RNG algorithm might change in the future, if you use that number to seed love's RNG anyway. It is only good for one high-quality number and would require work to generate more (look up sponge functions if you're interested). It is also very slow.
Code: Select all
local rand
do
local prefix
do
local d = os.date('!*t')
prefix = ("%d:%d:%d:"):format(d.year, d.month, d.day)
end
local ctr = 0
local pool = love.data.hash('sha512', prefix .. tostring(ctr))
local idx = 1
function rand(lo, hi)
assert(hi - lo >= 0 and hi - lo <= 0xFFFFFFFF, "rand: Invalid range for low and high values")
if idx > #pool then
ctr = ctr + 1
pool = love.data.hash('sha512', prefix .. tostring(ctr))
end
idx = idx + 4
return love.data.unpack("<i4", pool, idx - 4) % (hi - lo + 1) + lo
end
end