?
Solved

random numbers

Posted on 2005-04-27
3
Medium Priority
?
247 Views
Last Modified: 2012-06-22
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]
0
Comment
Question by:lukegriffiths110
3 Comments
 
LVL 18

Accepted Solution

by:
Billystyx earned 2000 total points
ID: 13881598
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
 
LVL 9

Expert Comment

by:rexmor
ID: 13881938
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
 
LVL 3

Expert Comment

by:y_o_y
ID: 13882618
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question