Closed tyetrask closed 5 months ago
Wow, nice catch. It seems like this is caused by rand() on Playdate only generating 31 bits of randomness instead of 32. I would prefer to use the system rand since it's probably better than the one I threw together, so I opened a PR here: https://github.com/finnvoor/PlaydateKit/pull/61 to only use the bottom half of rand(), which fixes the issue. Thanks for looking into this!
Thank you, @finnvoor!
(👋 This is (one of) my first contribution(s) to the repository so please let me know if I've missed any rules/guidelines/instructions or anything else I should do for this pull request, thank you!)
Description
While observing some random generation in a project, I noticed that seemingly not all of the values were being selected despite many calls to
Int.random()
,Float.random()
, or array's.randomElement()
. I know very little about C or lower level memory so my debugging skills are fairly limited but I was able to see thatarc4random_buf
implementation had been added and then later updated.As a test, I reverted the implementation to the original in my working fork of
PlaydateKit
and the issues resolved. I've tried to dig a bit as to why this might be happening and see if there's a fix for the newer implementation but this is out of my comfort zone for now.This pull request reverts to the original implementation (before this change).
Of course, maybe this isn't desired and there's a reason not to go back but I thought I'd at least open the pull request for discussion! I was definitely planning to see if I could spot anything the newer implementation but as mentioned, I need to do a bit of studying before I think I'll be able to make much headway there.
Reproduction
A 'game' to reproduce this is on a branch here but the simplest portion is something like this:
Current Behavior
With latest
main
for PlaydateKit, this produces the following:(Note the distinct lack of
3
s 😄, and this is the case seemingly no matter how long I let it run.)Expected Behavior
With the reversion to the original implementation, the same
Game
produces the following: