evaera / Cmdr

Extensible command console for Roblox developers
https://eryn.io/Cmdr/
MIT License
363 stars 104 forks source link

Update `rand` to use Random class #329

Open autonordev opened 2 months ago

autonordev commented 2 months ago

The rand command currently uses math.random to generate numbers. Math.random is not actually random; it does it use a very good source of entropy by default, and the common approach is to set the seed (which is global across the realm) to the UNIX timestamp (which is also not that good of a randomness source, and is not something we can expect to happen in modern games which are unlikely to use math.random in the first place).

This ticket proposes:

While simple, this does raise a question that I think need discussion before this is ready for work: should we use integers or decimals? IIRC with math.random it will use integers if the seed is an integer and decimals if the seed has a decimal point, but I think this is wrong. In-keeping with current behaviour would be to use integer, but the command will probably be more useful (e.g. for pairing with pick via meta-commands[^1]) with integers.

It's worth noting that even if we stick with decimals, there will be a change in behaviour, as developers will no longer be able to control the seed used (instead, it would be an internal source of entropy as with any other Random.new() calls). I can maybe imagine some use cases for this, but I expect it would be pretty rare. I think a note in the changelog about this is fine, and the change would be suitable in a minor (feature) release.

[^1]: pick is typed to use a positive integer, which should error if given a decimal, so the pick call in run echo My favourite fruit is ${pick ${rand 1 5} apples,pears,oranges,mangoes,bananas} should error.