sanitec
11-22-2010, 10:38 PM
Hi! I was given this exercise at school, and I'm not able to solve it! Could you guys give me a hand?
Create a function that creates 9 uniqe and random numbers in an Array, and present the Array for the user.
This is the code I've got so far:
function calculate() {
var counter:int = 0;
var counter2:int = 0;
var table:Array = new Array();
while (counter < 9) {
var num:uint = Math.floor((Math.random()*34)+1);
table.push(tall);
while(counter2 < table.length){
if(table[counter2] == num){
table.pop();
counter--;
}
counter2++;
}
counter++;
}
counter = 0;
while(counter < table.length){
trace(table[counter]);
counter++
}
}
calculate();
jsprclimber123
11-22-2010, 10:49 PM
Yikes. Way to complicated.
stop();
var table:Array = [];
var i:Number = 0;
for(i = 0; i < 9; i++)
{
var num:Number = Math.floor(Math.random()*34);
table.push(num);
trace(table);
}
This will give you 9 random numbers (denoted by i < 9 in the for loop statement) between 0 and 34 (denoted by multiplying Math.random() by 34). They are whole numbers due to the Math.floor() function.
Hope this helps.
Eralmidia
11-23-2010, 01:05 AM
jsprclimber123, remember, he needs unique numbers :)
var numbers:Array = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34);
var shuffle:Array = [];
while (numbers.length > 0) {
shuffle.push(numbers.splice(Math.round(Math.random () * (numbers.length - 1)), 1)[0]);
shuffle = shuffle.splice(0,8);
}
trace(shuffle) //example output: 9,17,4,26,16,28,27,18
Now this is just a quick example. Obviously, you can make this more dynamic and elegant, by, for instance, create a loop to push() in the values in the numbers array to avoid the hardcoded long array with all the numbers.
What this basically does, is that it creates a new array with the same values, but it shuffle them in random order. Then we simply pick the first 9 elements from the shuffled array giving us 9 random unique elements. Let me know if I misunderstood the assignment, but it seems this is what you're asking for :)
jsprclimber123
11-23-2010, 01:15 AM
Ah good call, forgot the unique part in my solution ;)
sanitec
11-23-2010, 05:57 AM
Thanks guys! I'm really new to programming, but that made sense to me! I'll try to da a loop to push the numbers into the array aswell
Eralmidia
11-23-2010, 06:54 AM
Good, I'm sure you'll manage. Just create a variable which holds the number of elements you want (in this case 34), and create loop that runs the number of times = to the var. For each iteration you create a new element in the array with the push() method, containing the value of the counter.
Let me know if you get any problems :)
sanitec
11-23-2010, 12:07 PM
I solved the problem with the same code i had. I just had to change the second loop from while(counter2 < table.length) to while(counter2 < counter)
I don't really know the splice function yet, but I guess we'll learn that later on.
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.