Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

Show random text when a button is clicked!?

[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

antonback89
Posts: 0
Joined: Mon Apr 14, 2014 7:17 pm

Show random text when a button is clicked!?

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

Igor
Posts: 0
Joined: Tue Apr 02, 2013 12:05 pm

Show random text when a button is clicked!?

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

antonback89
Posts: 0
Joined: Mon Apr 14, 2014 7:17 pm

Show random text when a button is clicked!?

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?

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

Show random text when a button is clicked!?

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

antonback89
Posts: 0
Joined: Mon Apr 14, 2014 7:17 pm

Show random text when a button is clicked!?

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?

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

Show random text when a button is clicked!?

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

Sunita
Posts: 0
Joined: Sat Nov 15, 2014 2:40 pm

Show random text when a button is clicked!?

Hi Maryna!

On what event will I have to place this code?

Return to “Issues”