View Full Version : shuffle numbers in text boxes
ez_stylin
January 20th, 2004, 01:19 PM
how do you shuffle random numbers?
lets say i hav 5 dynamic text boxes.. and i want to shuffle numbers 1 through 5 in each of them on a click of a button... how would i do that??
ScriptFlipper
January 20th, 2004, 03:00 PM
search for "random number" or similar here in the forums.
APDesign
January 20th, 2004, 03:27 PM
set the dynamic variable to randomNumber....
randomNumber = Math.ceil(Math.random()*5));
ez_stylin
January 20th, 2004, 03:46 PM
yes.. i know how to get random numbers..
but what i want to do is hav 5 dynamic text boxes, that has numbers 1 on box 1, 2 on box 2, goes on through 5, and then with a click of a button the numbers 1 - 5 wud look like they switch places with eachother on those 5 boxes...
ScriptFlipper
January 20th, 2004, 04:08 PM
1. Give each textfield an initial number.
2. Run the random thing for the first textfield.
3. ...and for the second textfield. verify against field 1.
4. random textfield 3. verify against 1&2.
5. random txtfield 4. verify against 1,2&3.
and so on.
the verify process could look like
while (textField2.text!==textField1.text) {
textField2.text = Math.ceil(Math.random()*5));
}
hope you understand...
claudio
January 20th, 2004, 04:14 PM
Place the 5 blank textfields on stage, and name them box1, box2, box3, box4 and box5. Name the button my_button.
Now place this code on a frame on the timeline:
numbers_array = [1, 2, 3, 4, 5];//array with the values to be placed into the textfields
Array.prototype.randomize = function() {
return this.sort(function (a, b) {
return (Math.floor(Math.random()*2) == 0) ? 1 : -1;
});
};
swapNumbers = function () {
for (var i = 0; i<5; i++) {
this["box"+(i+1)].text = numbers_array[i];
}
numbers_array.randomize();
};
my_button.onPress = function() {
swapNumbers();
};
swapNumbers();
ScriptFlipper
January 20th, 2004, 04:18 PM
but I think mine would have worked too, if I would have worked on it a little bit... :D
I didn't try it ;)
claudio
January 20th, 2004, 04:21 PM
I dont know, maybe your method do works, i havent tried it ;)
ScriptFlipper
January 20th, 2004, 04:23 PM
I think it does, though, yours require less code. (-:
kode
January 20th, 2004, 04:45 PM
Here's another method... :P
var numbers = [1, 2, 3, 4, 5];
function shuffle() {
var temp = numbers.slice();
while (temp.length) {
eval("box"+temp.length).text = temp.splice(Math.floor(Math.random()*temp.length), 1);
}
}
my_button.onPress = shuffle;
shuffle();
ScriptFlipper
January 20th, 2004, 05:01 PM
hey, slow down now, let's not bury ez_stylin in ways of doing this... ;)
kode
January 20th, 2004, 05:05 PM
The more options the better... don't you think? ;)
claudio
January 20th, 2004, 05:10 PM
Yes, the more options, the better. And Kax, you beat me using half the lines i used. :P
ScriptFlipper
January 20th, 2004, 05:12 PM
yup. it's great to get a lot of answers. I usually get. but my questions tend to be rather simple for you flash-gurus :D
kode
January 20th, 2004, 05:14 PM
Originally posted by claudio
Yes, the more options, the better. And Kax, you beat me using half the lines i used. :P
Uhmm... it wasn't my intention, but now that you mention it....
Haha! I beat ya!! :P
...Just kidding. :beam:
ez_stylin
January 20th, 2004, 05:42 PM
um thanx guys! il jus try em all.. lol
kode
January 20th, 2004, 05:51 PM
Good luck, ez_stylin! :)
GreenLantern
January 21st, 2004, 03:33 AM
Originally posted by kode
Here's another method... :P
var numbers = [1, 2, 3, 4, 5];
function shuffle() {
var temp = numbers.slice();
while (temp.length) {
eval("box"+temp.length).text = temp.splice(Math.floor(Math.random()*temp.length), 1);
}
}
my_button.onPress = shuffle;
shuffle();
Kode,
Could you comment this for me? I don't really understand how it works.
Thanks!
kode
January 21st, 2004, 04:23 AM
Originally posted by RedMirrorBall
Kode,
Could you comment this for me? I don't really understand how it works.
Thanks!
Sure! I'll try to explain it. :)
Firstly, we define the array containing the numbers:
var numbers = [1, 2, 3, 4, 5];
And now we define our function:
function shuffle() {
We use the Array.slice() method to create a copy of the array, so we can do anything we want without modifying the original array:
var temp = numbers.slice();
Now we create a loop with the while statement to execute the code until there are no more elements in the array:
while (temp.length) {
This line is a bit tricky... the eval() function will return a reference to the text field based on the string "box" and the number returned by the Array.length property; then we use the Array.splice() method to extract an element from the array and return its value so we can assign it to the text of our text field; the Math.floor() and Math.random() methods are used to generate a random number between 0 and one less than the length of the array. In definition, we create a reference to the text field, extract a random element from the array and assign it to the text of the text field:
eval("box"+temp.length).text = temp.splice(Math.floor(Math.random()*temp.length), 1);
We assign the function to the onPress event handler of the button:
my_button.onPress = shuffle;
And finally, we call the function:
shuffle();
...I really hope it makes sense. :P
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.