Page 3 of 3

Re: How to go about daily random numbers

Posted: Sun Jan 09, 2022 2:56 pm
by grump
Wow, that code borders strongly on gibberish territory ;)

A simple sponge function is much simpler and shorter:

Code: Select all

local function Sponge(seed)
	local state = love.data.hash('md5', tostring(seed))

	return function(lo, hi)
		lo, hi = math.min(lo, hi), math.max(lo, hi)
		state = love.data.hash('md5', state)
		return (love.data.unpack('<i6', state)) % (hi - lo + 1) + lo
	end
end
md5 because it's probably the fastest hash function, and using your unpack idea. Not sure about periodicity and the 'i6' thing.

Code: Select all

local d = os.date('!*t')
local rng = Sponge(("%d:%d:%d:"):format(d.year, d.month, d.day))
for i = 1, 10 do
	print(rng(1, 10))
end

Re: How to go about daily random numbers

Posted: Sun Jan 09, 2022 4:31 pm
by pgimeno
grump wrote: Sun Jan 09, 2022 2:56 pm Wow, that code borders strongly on gibberish territory ;)
Why?
grump wrote: Sun Jan 09, 2022 2:56 pm A simple sponge function is much simpler and shorter:
Well, that's not a true sponge function, as the initialization does not "absorb" the input (seed) in the way that sponge functions do. It's just an iterated hash. Also since a hash is not a [edit: cyclic] permutation function, there's no guarantee that you won't hit a short cycle.
grump wrote: Sun Jan 09, 2022 2:56 pm md5 because it's probably the fastest hash function, and using your unpack idea. Not sure about periodicity and the 'i6' thing.
I used sha512 because it produces the most output per call, so it avoids as many calls as possible. I imagined that one sha512 would be faster than four md5's. But in a benchmark, in isolation, that turns out not to be the case, by a tight margin: four MD5s are about 3% faster. I also checked SHA1; three SHA1s are about 45% slower than one SHA512, and produce less output.

The problem of the periodicity is that it's unpredictable; you'd need a cyclic permutation function instead of a hash, to guarantee maximum period. The counter method is guaranteed to have a period as large as the counter can reach.

i6 is OK.

Re: How to go about daily random numbers

Posted: Sun Jan 09, 2022 6:49 pm
by grump
pgimeno wrote: Sun Jan 09, 2022 4:31 pm
grump wrote: Sun Jan 09, 2022 2:56 pm Wow, that code borders strongly on gibberish territory ;)
Why?
It was just hard on the eyes at first glance and difficult to figure out what's going on.

It's arguably still a sponge, just simplified. I didn't include the slow absorption step and used an empty initial state because I think it's not necessary for this, as it does not affect the quality of the generated numbers and there's no need for a hidden state.

Finding a "short" cycle in the generated sequence seems extremely improbable, but proving that point is beyond my expertise.

Re: How to go about daily random numbers

Posted: Sun Jan 09, 2022 7:02 pm
by BrotSagtMist
This all looks so complicated, whats wrong with just
rng=math.floor(os.time()/(24*60*60)) ?

Re: How to go about daily random numbers

Posted: Sun Jan 09, 2022 7:12 pm
by Gunroar:Cannon()
Image

Re: How to go about daily random numbers

Posted: Thu Jan 20, 2022 5:32 pm
by Jasoco
BrotSagtMist wrote: Sun Jan 09, 2022 7:02 pm This all looks so complicated, whats wrong with just
rng=math.floor(os.time()/(24*60*60)) ?
This is actually how I always thought these games with "daily challenges" worked anyway. It would be simple, would be standard across all platforms as long as the RNG is also uniform, and wouldn't require a server so it's future proofed in case the servers that store the high scores ever goes offline you can still have a daily seed for community purposes.

Basically take the time, divide it by the amount of seconds in a day and find the current day number. Simple as can be.

If I ever make a game with daily challenges, I'd be using this method.

Re: How to go about daily random numbers

Posted: Fri Jan 21, 2022 12:33 am
by Gunroar:Cannon()
Nice, that makes sense I suppose since there's just a need for a seed.

Re: How to go about daily random numbers

Posted: Tue Jan 25, 2022 3:49 pm
by Jasoco
Gunroar:Cannon() wrote: Fri Jan 21, 2022 12:33 am Nice, that makes sense I suppose since there's just a need for a seed.
It's really all you need, and since normally a seed for games like those is just chosen from the current second, it makes sense to also choose the daily seed from the current second at the stroke of midnight for that day. (GMT of course. That's how all the games I've seen do it. They all change over at 7PM eastern, my time.)

A separate server would of course be used for high score storage if you want it to be, but if I was developing those games I would make sure the Daily Challenge doesn't require an active server just to play as that would suck if sometime down the line they decide to shut down the high score servers for, say, Spelunky, and people in the community couldn't do dailies anymore even for fun. (Note: I don't know if Spelunky does require a server ping to even let you play a Daily. It probably does require an online connection though. I am just saying how I would design it. I'd want my community to be able to enjoy the game their own way well into the future and if I have to turn off the servers for any reason, I'd want them to still be able to get together and play daily challenges as a community and keep their own records of their scores. So I'd build in a fallback just in case of server apocalypse. lol)