dimanche 18 janvier 2015

Generate Unique Random Numbers in Livecode

I have created a new stack with six buttons (five small and one big) on card1. In every button there is a number like this.



button1 - 1
button2 - 2
button3 - 3
button4 - 4
button5 - 5


When I click the big button, I want to exchange these numbers randomly similar to this...



button1 - 4
button2 - 5
button3 - 1
button4 - 2
button5 - 3


After clicking the button again...



button1 - 4
button2 - 3
button3 - 5
button4 - 2
button5 - 1


And every time I click the big button again and again, the numbers are exchanged.


I tried this script for onmouseup handler on the big button but it is not the right way because sometimes it causes process delay.



put random(5) into num1
put random(5) into num2
put random(5) into num3
put random(5) into num4
put random(5) into num5

repeat until num2 is not num1
put random(5) into num2
end repeat

repeat until num3 is not num1 and num3 is not num2
put random(5) into num3
end repeat


repeat until num4 is not num3 and num4 is not num2 and num4 is not num1
put random(5) into num4
end repeat

repeat until num5 is not num4 and num5 is not num3 and num5 is not num2 and num5 is not num1
put random(5) into num5
end repeat

put num1 to button "button1"
put num2 to button "button2"
put num3 to button "button3"
put num4 to button "button4"
put num5 to button "button5"


What is the correct way to do that?




Additional: Is there any way to generate random numbers WITH exceptions?





Aucun commentaire:

Enregistrer un commentaire