# random numbers

i ve got this code from a website but i can't seem to generate 6 random numbers between 1 and 49, can some1 have a look at the code and see what needs changing. thanks

function getUniqueRandom (number) {
var numberlist = new array();
for (count = 1; count <= number; count++) {
numberlist[count] = count;
}
this.rand = new array();
for (count = numberlist.length - 1; count > 0; count--) {
picked = math.floor(math.Random()*count)+1;
this.rand[count-1] = numberlist[picked];
numberlist[picked] = numberlist[count];
}
return rand;
}

num = new getUniqueRandom(6);

Rand0 = num.rand[1]
Rand1 = num.rand[2]
Rand2 = num.rand[3]
Rand3 = num.rand[4]
Rand4 = num.rand[5]
Rand5 = num.rand[6]
###### Who is Participating?

Commented:
hello luke, did you try this?
myArr=[];
function shuffle() {
if (random(50)) {
return 1;
}
}
for(var i = 1; i < 50; i++){//this section changed...
if(i<10){
myArr.push("0"+i);
}else{
myArr.push(i);
}
}
myArr.sort(shuffle);
for(i=0;i<6;i++){
_root["Rand"+i]=myArr[i];
trace("_root[Rand"+i+"]="+_root["Rand"+i]);
}

Billystyx
0

Commented:
hello,

try also this one...

function randomnumbers(lowest, highest, count) {
var randomnums = new Array();
if (count<=highest-lowest) {
var nums = new Array();
for (var i = lowest; i<=highest; ++i) {
nums.push(i);
}
for (var i = 1; i<=count; ++i) {
var randomnumber = Math.floor(Math.random()*nums.length);
randomnums.push(nums[randomnumber]);
nums.splice(randomnumber, 1);
}
}
return randomnums;
};
myrandom = randomnumbers(1, 49, 6);
trace(myrandom);

0

Commented:
Hi,

try this one...actually there not much changes on your code..it's just some of  your keywords are incorrectly typed..like "math.floor" it should be Math.floor....k just see this...

function getUniqueRandom (number) {
var numberlist = new Array();
for (count = 1; count <= number; count++) {
numberlist[count] = count;
}
this.rand = new Array();
for (count = numberlist.length - 1; count > 0; count--) {
picked = Math.floor(Math.random()*count)+1;
this.rand[count-1] = numberlist[picked];
numberlist[picked] = numberlist[count];
}
return rand;
}

num = new getUniqueRandom(6);

Rand0 = num.rand[1])
Rand1 = num.rand[2]
Rand2 = num.rand[3]
Rand3 = num.rand[4]
Rand4 = num.rand[5]
Rand5 = num.rand[6]

best regards!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.