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
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 -
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;
-
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} });
-
Are you really sure Random is working ?
-
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.
-
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) -
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.
-
That's great :D
I'm waiting for your final code. -
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.