How to go about daily random numbers

General discussion about LÖVE, Lua, game development, puns, and unicorns.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: How to go about daily random numbers

Post 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
User avatar
pgimeno
Party member
Posts: 3541
Joined: Sun Oct 18, 2015 2:58 pm

Re: How to go about daily random numbers

Post 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.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: How to go about daily random numbers

Post 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.
User avatar
BrotSagtMist
Party member
Posts: 604
Joined: Fri Aug 06, 2021 10:30 pm

Re: How to go about daily random numbers

Post by BrotSagtMist »

This all looks so complicated, whats wrong with just
rng=math.floor(os.time()/(24*60*60)) ?
obey
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: How to go about daily random numbers

Post by Gunroar:Cannon() »

Image
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: How to go about daily random numbers

Post 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.
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: How to go about daily random numbers

Post by Gunroar:Cannon() »

Nice, that makes sense I suppose since there's just a need for a seed.
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: How to go about daily random numbers

Post 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)
Post Reply

Who is online

Users browsing this forum: No registered users and 19 guests