Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

6 random numbers 0-49

Posted on 2005-04-27
4
Medium Priority
?
295 Views
Last Modified: 2011-10-03
Basically i dont want rand0 being the same as rand1 or rand2 etc i want six different randoly generated numbers from 01-49 its nearly there the code just needs some more work. Any help much appreciated.


rand1st();
function rand1st(){
Rand0="";
i = 1;
while (Number(i)<>3) {
     rnd = random(5);
     x = 1;
      while (Number(x)<>Number((Number(length(Rand0))+1))) {
          if ((substring(Rand0, x, 1)) eq rnd) {
               rnd = "";
          }
          x = Number(x)+1;
     }
     if (rnd ne "") {
          Rand0 = Rand0 add rnd;
          i = Number(i)+1;
     }
}
}
Rand1="";
i = 1;
while (Number(i)<>3) {
     rnd = random(5);
     x = 1;
      while (Number(x)<>Number((Number(length(Rand1))+1))) {
          if ((substring(Rand1, x, 1)) eq rnd) {
               rnd = "";
          }
          x = Number(x)+1;
     }
     if (rnd ne "") {
          Rand1 = Rand1 add rnd;
          i = Number(i)+1;
     }
}
Rand2="";
i = 1;
while (Number(i)<>3) {
     rnd = random(5);
     x = 1;
      while (Number(x)<>Number((Number(length(Rand2))+1))) {
          if ((substring(Rand2, x, 1)) eq rnd) {
               rnd = "";
          }
          x = Number(x)+1;
     }
     if (rnd ne "") {
          Rand2 = Rand2 add rnd;
          i = Number(i)+1;
     }
}
Rand3="";
i = 1;
while (Number(i)<>3) {
     rnd = random(5);
     x = 1;
      while (Number(x)<>Number((Number(length(Rand2))+1))) {
          if ((substring(Rand2, x, 1)) eq rnd) {
               rnd = "";
          }
          x = Number(x)+1;
     }
     if (rnd ne "") {
          Rand2 = Rand2 add rnd;
          i = Number(i)+1;
     }
}
Rand4="";
i = 1;
while (Number(i)<>3) {
     rnd = random(5);
     x = 1;
      while (Number(x)<>Number((Number(length(Rand2))+1))) {
          if ((substring(Rand2, x, 1)) eq rnd) {
               rnd = "";
          }
          x = Number(x)+1;
     }
     if (rnd ne "") {
          Rand2 = Rand2 add rnd;
          i = Number(i)+1;
     }
}
Rand5="";
i = 1;
while (Number(i)<>3) {
     rnd = random(5);
     x = 1;
      while (Number(x)<>Number((Number(length(Rand2))+1))) {
          if ((substring(Rand2, x, 1)) eq rnd) {
               rnd = "";
          }
          x = Number(x)+1;
     }
     if (rnd ne "") {
          Rand2 = Rand2 add rnd;
          i = Number(i)+1;
     }
}
if(rand1==rand0){
rand1st();
}
0
Comment
Question by:lukegriffiths110
  • 2
4 Comments
 
LVL 27

Accepted Solution

by:
negatyve earned 2000 total points
ID: 13876452
// function to shuffle an array
function shuffleArray(list, max)
{
      var a = list.slice();
      var b = new Array();
      for (var i = 0; i < (max != undefined && max <= list.length ? max : list.length); i++) {
            var n = random(a.length);
            b.push(a[n]);
            a.splice(n, 1);
      }
      return b;
}
// create your unique number array
var startArray = [];
// feed array with numbers from 1 to 49
for(var i = 1; i < 50; i++){
      startArray.push(i < 10 ? "0" + i : String(i));      
}
// shuffle your array asking for just 6 numbers
startArray = shuffleArray(startArray, 6);

Rand1 = startArray[0];
Rand2 = startArray[1];
Rand3 = startArray[2];
Rand4 = startArray[3];
Rand5 = startArray[4];
Rand6 = startArray[5];
0
 
LVL 18

Expert Comment

by:Billystyx
ID: 13878411
...and same again...
myArr=[];
function shuffle() {
      if (random(50)) {
            return 1;
      }
}
for(var i = 1; i < 50; i++){
     myArr.push(i);    
}
myArr.sort(shuffle);
for(i=0;i<6;i++){
_root["Rand"+i]=myArr[i];
}
(same as negatyves I think but shorter:)
Billystyx
0
 
LVL 18

Expert Comment

by:Billystyx
ID: 13878450
inside bit should look like 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];
}


Billystyx
(not much different now...)
0
 
LVL 9

Expert Comment

by:rexmor
ID: 13882029
hmmnn seems like repeated question... anyway... try this...

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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

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…
While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
Suggested Courses

571 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