Titanium Community Questions & Answer Archive

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

Some type of "Math.random()" command?

Is it possible to have some sort of

math.random()

command with Titanium? Or something like that? (Where it randomly chooses between values you set).

I need it to choose a number between 1 and 5 and then have it do what I want for each. I know Lua and PHP have this, but I don't know if JavaScript does (Or if Titanium, rather).

— asked September 19th 2010 by Colton Arabsky
  • math.random
0 Comments

2 Answers

  • As the example above shows, the most common way is:

    randomnumber = Math.floor(Math.random() * (highestNumberWanted + 1));

    Another commonly used method is:

    randomnumber = new Date().getTime() % (highestNumberWanted + 1);

    — answered September 20th 2010 by Mike Robinson
    permalink
    0 Comments
  • http://www.w3schools.com/js/js_obj_math.asp

    — answered September 19th 2010 by Dan Tamas
    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.