Generate Random Number
Im trying to generate a random number using the following with no success any help?
var ran1 = Math.floor(Math.random()*101)
^Not working
4 Answers
-
Hi Scott,
I use this function to produce a random number between two values. On the iPhone I have found that the Randomness isnt quite as random as I would like. It tends to change once every hour as the random function appears to take it's seed from the current time. No such problem on the Android version tho.
function randomXToY(minVal,maxVal) { var randVal = minVal+(Math.random()*(maxVal-minVal)); return Math.round(randVal); }
Hope that this helps.
Greg -
I'm having this same issue with iPhone development. I'm trying to generate four random numbers, however the numbers seem to be the same every time. This seems to be a huge bug in Titanium for anything (like game development) which relies on generating random numbers and makes me concerned that there are other bugs that exist in Titanium.
If the randomness changes every hour on the iPhone (vs every time you request a random number), how do developers address this issue??
-
create a string of values, then reverse the string and pull from the front, or simply pull from the end.
-
multiple the math.random times an md5 hash of the current time