Titanium Community Questions & Answer Archive

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

Random Generated Number/array

Hey people here.

Currently i'm developing an app which needs a random function or atleast something that shuffles data.

Now I've been searching on here for a week now and tried lots of posted codes and created code based of information.

I'm aware that the function either is bugged or rather difficult to get working.
The app is for the iPad and so far I've figured out it needs to use the currentTime (getTIme) to create seeds and output that into a random function.

currently the only suggestion that worked for me found in the Q&A was"

for (var i=0; i<50; i++) { var seed = new Date().getTime(); var random_number = Math.round((Math.random(seed*9301+49297) % 233280)*12); Ti.API.info ('random : '+random_number); }

This works for the part, except the first 5 numbers are ALWAYS the same.

So I'm asking here for help and maybe a clarification.
Cause it seems this function is either really difficult to get working or its extremely bugged?

Does anybody have a idea or a pre written code that is perfect for randomizing data?

Many Thanks

— asked September 23rd 2010 by Patrick van Zadel
  • 1.4.1.1
  • ipad
  • macosx
  • mobile
  • titanium
0 Comments

8 Answers

  • Code (generates a number between 0 and 99, ):

    var randomnumber = 0;
    
    for(var i = 0; i < 20; i++){
        randomnumber=Math.floor(Math.random()*100);
        Ti.API.info("random: " + i + " / " + randomnumber);
    }
    

    Output:

    [INFO] random: 0 / 38
    [INFO] random: 1 / 90
    [INFO] random: 2 / 40
    [INFO] random: 3 / 41
    [INFO] random: 4 / 3
    [INFO] random: 5 / 96
    [INFO] random: 6 / 32
    [INFO] random: 7 / 75
    [INFO] random: 8 / 17
    [INFO] random: 9 / 73
    [INFO] random: 10 / 24
    [INFO] random: 11 / 17
    [INFO] random: 12 / 65
    [INFO] random: 13 / 33
    [INFO] random: 14 / 30
    [INFO] random: 15 / 6
    [INFO] random: 16 / 32
    [INFO] random: 17 / 82
    [INFO] random: 18 / 51
    [INFO] random: 19 / 99
    
    — answered September 27th 2010 by Carl Jahn
    permalink
    2 Comments
    • Hey Carl,

      Thanks for the reply, The code indeed works, just as the last ones, to further explain my problem.
      I recorded and uploaded a video..as you can see the first 3/4 numbers are either always the same or grow incremental.

      here is the video:
      http://www.youtube.com/watch?v=qKabAm3UnpY

      — commented September 27th 2010 by Patrick van Zadel
    • Im having the same problem. It seems random but if you run it a bunch of times from scratch you get the same set of numbers. It seems to change every hour or so. It seems appcelerator is cutting the default time seed from millisecond to hours.. or something.

      — commented November 26th 2010 by Jonathon Beschen
  • Nobody here has any clue?

    Maybe we should try something different.
    Instead of a random function, how about a shuffle?

    Let us define the numbers and data for that number and let a function shuffle those around then output them accordingly.

    Any anwser for random is still welcome though.

    — answered September 27th 2010 by Patrick van Zadel
    permalink
    0 Comments
  • Hi Patrick,
    I cut/pasted and ran the code you have above, and at least on the simulator I don't see the first 5 numbers always the same as you reported. Gregor Munro had a nice post on this thread (http://developer.appcelerator.com/question/20901/random-number-bug) with a method for generating random numbers in a given range.

    — answered September 23rd 2010 by Christopher Rumpf
    permalink
    1 Comment
    • Thanks for your anwser.

      But if you look closely, this is one of the Codes that Gregor posted.
      The one that is recommende twice always gives me back the number 30
      everytime.

      I put the code in a clean project so there isn't any interference.

      I wonder what the problem is, why do I always get the Same 5 numbers in my posted code and why do I always get the same number in the recommended Gregor post.

      Also, that Topic you post is the first I could find and tried every bit of code in there.
      None of them work

      — commented September 23rd 2010 by Patrick van Zadel
  • I know you have tried many things, maybe even the one I am providing, but it seems to have worked pretty well for me and isn't an obvious choice…..

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

    Though this may not work so well in your case, being as you are continually looping…though you could throw some more randomness into it.

    — answered September 27th 2010 by Mike Robinson
    permalink
    2 Comments
    • Hey Mike,

      This code workst perfect on itself indeed, and I can definitely do something with this.

      I'm beginning to think it has something to do with my simulator :o
      When I use the code you provided, I do get a random number everytime.

      but when I put it in this code

      `
      var randomnumber = 0;

      for(var i = 0; i < 20; i++){
      randomnumber = new Date().getTime() % (24+ 1);
      Ti.API.info("random: " + i + " / " + randomnumber);
      }
      `
      the numbers are again incrementing.

      it comes as

      1 / 19
      2 / 19
      3/ 19
      4/ 19
      5 / 19
      6 / 20
      7 / 20
      8 / 20
      9 / 20
      10 / 20

      etc etc etc

      any ideas on this.

      also can they make a API/Module of the xcode SDK "arc4random()" function?

      — commented September 28th 2010 by Patrick van Zadel
    • Nevermind the error above.
      I wasn't looking clearly new day and new chances showed me I was blind XD

      The code above obviously doesnt work, because it gives me modulo way of outputting the number.
      It debugs so fast, that it gives me a number 6 times a second, which explains 6 times the number 19…

      Sorry about that, need to rebuild this code, maybe with a wait or something.
      outputting a number and wait like 832 miliseconds or something?

      — commented September 29th 2010 by Patrick van Zadel
  • I spent hours pulling my hair out over this one.

    The solution I came up with was to generate a string of random numbers, then reverse the string, and pull the data from the front of the string.

    
    str = str.reverse();
    
    — answered March 29th 2011 by Anthony DOnofrio
    permalink
    0 Comments
  • What about …

                    for (var z=0;z<30;z++)    
            {
                var chars = "0123456789ABCDEF";
                var string_length = 16;
                var randomstring = '';
                for (var i=0; i<string_length; i++) {
                    var rnum = Math.floor(Math.random() * chars.length);
                    randomstring += chars.substring(rnum,rnum+1);
                }
                var row = Ti.UI.createTableViewRow({height:50, hasCheck:false});
                var l1 = Ti.UI.createLabel({text:randomstring, font:{fontSize:14}, color:'#324892', left:10});
                row.add(l1);
                data.push(row);
            }
    
    — answered May 9th 2011 by Paul Nelson
    permalink
    0 Comments
  • 
    var live=new Date();
    live=live.toString();
    var myhash=Titanium.Utils.md5HexDigest(live);
    
    — answered May 9th 2011 by Josh Lewis
    permalink
    0 Comments
  • I have no way to justify this solution, but it works for me. Going along with the trend of the first 5 are the same, just call a function 5 times to get them out of the way and the rest actually appear to be random every time.

    function genRandNum(max){ &#x2F;&#x2F;could be more robust, this returns (zero through max) return Math.floor(Math.random()*(max-1)); }

    then hack in this above your script:

    var null_rand1 = genRandNum(10); var null_rand2 = genRandNum(10); var null_rand3 = genRandNum(10); var null_rand4 = genRandNum(10); var null_rand5 = genRandNum(10);

    no idea why, but works in my app.

    — answered August 19th 2011 by Jim Carter III
    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.