Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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

— asked March 20th 2010 by scott castov
  • iphone
  • java
0 Comments

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

    — answered March 20th 2010 by Gregor Munro
    permalink
    1 Comment
    • Hi Gergor,

                Though you have answered this 4 years ago..it stills helping newbie like me,
      

      Thanks for your contribution

      — commented October 5th 2013 by Chetan Chaudhari
  • 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??

    — answered April 28th 2010 by Andrew Charon
    permalink
    0 Comments
  • create a string of values, then reverse the string and pull from the front, or simply pull from the end.

    — answered March 29th 2011 by Anthony DOnofrio
    permalink
    0 Comments
  • multiple the math.random times an md5 hash of the current time

    — answered March 29th 2011 by Josh Lewis
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.