I’m having some issues with random numbers in Unity iPhone.
I want my App to play a random sound when it’s started, so I’ve put this line in my Audio Object script:
SoundChoice = Mathf.Round((Random.value*19)+1);
This should give me a number between 1 and 20.
It works fine in the editor - every time I start the app, a different sound starts, but the problem is that when I upload it to my iPhone, the app seems to “Choose” a random number and then just uses that number every time the app is used until I re-install the app.
That ain’t cool!
I’m pretty sure I’m doing everything right (hey, it all works A-OK in the editor!), so is there a gotcha I’m missing which would mean it wouldn’t work on the device?
hm, never noticed that, although i use Random.Range() - maybe it works better.
but anyway, if this happens, you have to change the seed (Random.seed) - on other systems, you usually take the system timestamp, but i don’t know how to get it in unity. maybe saving a random number in preferences and use that random number as seed on next startup might help.
Random.Range() works perfectly for me . Surprisingly well actually considering I am not even setting the seed value!
edit:
whoops I was wrong. I am getting roughly the same initial values on the device. Need to seed. This is just how pseudo-random number generators work.
I’m not sure the seed is chosen automatically by that function. But Random numbers seem to be better in the editor for some reason. Maybe it’s because the game engine is being loaded and scripts have run, even before you run your level in the editor. I know that in 2.5 the whole UI is written in Unityscript. Just guessing this has something to do with it in Unity iPhone as well.
I was thinking about the accelerometer and it’s kind of like having a built-in entropy generator
this should work for seeding based on current time on the iphone (haven’t tested it on actual hardware yet)
Random.seed = System.DateTime.Now.Ticks;
should seed with the number of 100-nanosecond ticks that have elapsed from some date - either jan 1 1970 or jan 1 0001…
I actually like that the RNG isn’t seeded for us - makes testing some things a bit easier - and also allows you to seed with ‘known’ seeds to get repeatable psuedo-random behaviour, if you want it.
I didn’t have a chance to play with random numbers yet, but have this task in my tasklist. It’s always better to bugreport if you want a bugfix or more or less official statement about bug. And bugreports have higher priority then forum things now.
Random works fine- I think there was just a question about seeding. What I found is in the editor maybe it is seeding automatically but on the device it’s not?
No. The only reason to use Random.seed is if you want a repeatable set of “random” numbers; otherwise don’t touch it. Not sure how it’s seeded internally; probably time/date or something, but regardless, if you just want ordinary random numbers, the randomness is fine as-is.