luxeengine / alpha

alpha - deprecated 2015~2016. unrelated to the new engine! view the new engine here - https://luxeengine.com/
MIT License
565 stars 74 forks source link

Random; A negative seed will give negative random values #298

Closed smvilar closed 9 years ago

smvilar commented 9 years ago

I ran into this by mistake, but I think it's worth fixing it by adding a Math.abs(seed) to the constructor or something like that.

NicoM1 commented 9 years ago

also smaller seeds seem to cause poor generation, not sure if this is intentional, I note when you set it up in the helper class you have to multiply the seed to a much larger number.

Nico

On Tue, Jul 28, 2015 at 7:55 PM, Santiago V. notifications@github.com wrote:

I ran into this by mistake, but I think it's worth fixing it by adding a Math.abs(seed) to the constructor or something like that.

— Reply to this email directly or view it on GitHub https://github.com/underscorediscovery/luxe/issues/298.

ruby0x1 commented 9 years ago

@smvilar which platform were you testing on and do you construct with your own seed or use Luxe.utils.random

smvilar commented 9 years ago

@underscorediscovery web, with my own seed

smvilar commented 9 years ago

that didn't sound weird at all

ruby0x1 commented 9 years ago

HAHA, thanks for the info @smvilar, I'd be curious what you're using for the seed, as it's possible that it wraps around from being too big.

smvilar commented 9 years ago

Yes, I think that a big number may be the case. I'm using a string converted to an int with a crc32 hash (btw if you know a better way to do this please tell me). That's because I want all the "random" generated stuff in the game to be dependent on the username. So yeah maybe it was a really big number, but I think it was a negative seed because I added Math.abs(seed) and it solved it.

ruby0x1 commented 9 years ago

I think that a negative seed is quite possible and will return negative results but I am mainly considering whether or not forcing that value to be mutated is a good idea. For example if you generate the seed and print it and then print the random seed they will differ, and this will cause headaches if you don't understand why.

The other alternative is an assertion on negative seeds, but I also see that as invasive.

For the string conversion, you could try Luxe.utils.hash('username') (which uses djb2 currently, so that may not be ideal) but it returns unsigned value meaning it can't be negative and will fit in the same size as the seed should without wrapping. I don't think it's better per se, just wanted to note there are some options for unique id's and hashes there that might suit something.

smvilar commented 9 years ago

I find it really annoying that it gave me negative values, since I was expecting a number to use it as an array index, and when it returned a negative value (outside the expected range) it generated an out of bounds exception! It wasn't clear why the game was failing at first glance, and I had to debug it for a while until I discovered the negative seed.