Page 2 of 2

Show random text when a button is clicked!?

Posted: Wed Apr 16, 2014 7:00 pm
by Maryna Brodina

[quote:]What does "var rand = min - 0.5 + Math.random()*(max-min+1)" do?[/quote] this line retrieves random value from "min" to "max"[quote:]Is "var min = 0, max = 2;" the length of my list?[/quote]in this case it starts from 0, could be also 0,1,2.
3) the chance to get each text is 1/3


Show random text when a button is clicked!?

Posted: Wed Apr 16, 2014 9:07 pm
by antonback89

Okey thank u so much!
But the "min - 0.5 + math.random" (max-min+1) what is +1? =)


Show random text when a button is clicked!?

Posted: Thu Apr 17, 2014 2:50 am
by Igor

Hello,

+1 is a part of the formula to get random number from "min" to "max".

We have wrote a code that you can use without understanding this formula and from what parts it consists.

You need only fill "arr" array with your text elements and set your label name.

pre
//Just add here new elements you need.
var arr = ["Text1", "Test2", "Test3", "you", "can", "add", "any", "text", "you", "wish"];

//this code not need to be changed.
var min = 0, max = arr.length - 1;
var rand = min - 0.5 + Math.random()*(max-min+1)
rand = Math.round(rand);

//Here is have to change "mobilelabel_7" by your label name.
Appery('mobilelabel_7').text(arr[rand]);

/pre


Show random text when a button is clicked!?

Posted: Thu Apr 17, 2014 11:55 am
by antonback89

Thank you so much!

So if i put a bigger a list (let's say 80 items) the chances to get same twice is gonna bea 1/80. I don't have to change any number or so in my code, except for max to 85?


Show random text when a button is clicked!?

Posted: Thu Apr 17, 2014 4:08 pm
by Maryna Brodina

Hello!

In the last code max = arr.length-1; there is automatically counting array length. So you need to fill in array with your values. Chance text is shown is equal to 1/arr.length


Show random text when a button is clicked!?

Posted: Fri Apr 18, 2014 7:34 pm
by antonback89

Thank you very much!
Now my code looks like this:

var min = 0, max = arr.length-1;
var rand = min - 0.5 + Math.random()*(max-min+1);
rand = Math.round(rand);
Appery('mobilelabel_13').text(arr[rand]);

Does that seem correct?


Show random text when a button is clicked!?

Posted: Fri Apr 18, 2014 7:48 pm
by Maryna Brodina

Please try if it works for you and let us know if there are any issues.


Show random text when a button is clicked!?

Posted: Wed Mar 04, 2015 11:10 pm
by Sunita

Hi Maryna!

On what event will I have to place this code?