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 Label message

Hi all

Trying to create a random message on my home screen. However, i get the same message each time?

var safeMsg= [];
    safeMsg[0] = "Message one";
    safeMsg[1] = "Message Two";
    safeMsg[2] = "Message three";
    safeMsg[3] = "Message Four";
var l = safeMsg.length;
var sMRan = Math.floor(Math.random()*l);
var Msg = safeMsg[sMRan];

Anybody able to point out this school boy error or is it not possible?

thanks

— asked April 14th 2010 by Mark Pierce
  • iphone
  • lable
  • view
0 Comments

9 Answers

  • It should work.
    Try this:

    function randomint(min, max)
    {
       return Math.round(min + Math.random()*(max - min))
    }
    
    var sMRan = randomint(0, safeMsg.length);
    

    EDIT: Sorry, I just realised It's exactly what you've done …
    I must be tired -_-'
    So It really should work, because It works for me…
    Just try to switch floor and round

    — answered April 14th 2010 by Pierre Sabb
    permalink
    0 Comments
  • I have found some platforms don't support Math.random. You can make your own pseudo-random function based on for example the timestamp. Untested:

        var rand=new Date().getTime()%max;
    
    — answered April 14th 2010 by Martijn Pannevis
    permalink
    1 Comment
    • This is the best answer for the iphone as far as I've been able to tell. If you are using an automated roller its going to hurt b/c the numbers will be near sequential.

      Also dont forget to add '+ minVal' to the end or its 0-max.

      — commented November 26th 2010 by Jonathon Beschen
  • Hi Martijn and Pierre

    Thanks for you response.

    when I use Math.round i get no text at all and with Math.floor I only get the last message in the array. weird!

    this is the label showing the text.

    
    var footerLabel = Ti.UI.createLabel({
        text:Msg,
        color:'white',
        width:'auto',
        height:'auto',
        textAlign:'center',
        font:{fontWeight:'bold',fontSize:14}
    });
    
    — answered April 14th 2010 by Mark Pierce
    permalink
    0 Comments
  • Are you really sure Random is working ?

    — answered April 16th 2010 by Pierre Sabb
    permalink
    0 Comments
  • Hi Pierre

    Unless when I start the app it is randomly selecting the same thing. I have started my app 20 times and the same message displays. Always the last message of the array.

    floor is not working and round is not showing any message.

    — answered April 16th 2010 by Mark Pierce
    permalink
    0 Comments
  • Hi Mark,

    I mean, what happen if you try this:

            var alertDialog = Titanium.UI.createAlertDialog({
                title: 'random test',
                message: Math.random(),
                buttonNames: ['OK']
            });
            alertDialog.show();
    

    Does it display different numbers each time you lunch your app ?
    I believe the problem comes from this function … ('f course)

    — answered April 16th 2010 by Pierre Sabb
    permalink
    0 Comments
  • HI Pierre

    That works. i then tried this:

    message: Math.round(Math.random()),
    

    And it gave me 0 which is correct.

    then this:

    var h = 4;
    var alertDialog = Titanium.UI.createAlertDialog({
             title: 'random test',
             message: Math.round(Math.random()*h),
             buttonNames: ['OK']
     });
     alertDialog.show();
    

    And this just gives me the value 2! everytime. And i thought this would be the easy part ;) Thanks for your help so far. I will not be beat and will post the final solution.

    — answered April 17th 2010 by Mark Pierce
    permalink
    0 Comments
  • That's great :D
    I'm waiting for your final code.

    — answered April 17th 2010 by Pierre Sabb
    permalink
    0 Comments
  • Just saw your post.

    I had a similar problem with iPhone vs Android. The Android worked the iPhone did not appear to… However I discovered that the random function was working, just not randomly enough on the iPhone.

    From what I can tell, the Math.random() function gets its seed from the clock. So although the following code worked ok in most instances, it didnt work particularly well for the iPhone.

    function randomXToY(minVal,maxVal){ 
     var randVal = minVal+(Math.random()*(maxVal-minVal)); 
     return Math.round(randVal); 
    }
    
    var Msg = safeMsg[randomXToY(0,safeMsg.length-1)];
    

    As far as I can tell, the best way forward is to write your own psuedo randomiser.

    — answered April 27th 2010 by Gregor Munro
    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.