[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# duplicated numbers

Posted on 2005-04-27
Medium Priority
177 Views
Basically i dont want rand0 being the same as rand1 or rand2 etc i want six different randoly generated numbers from 01-49 it is working however duplicated numbers are comeing up. Any help much appreciated.

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 "") {
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 "") {
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 "") {
i = Number(i)+1;
}
}
0
Question by:lukegriffiths110
• 2

LVL 18

Accepted Solution

Billystyx earned 2000 total points
ID: 13875701
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 "") {
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 "") {
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 "") {
i = Number(i)+1;
}
}

if(rand1==rand0){
rand1st();
}

billystyx
0

LVL 27

Expert Comment

ID: 13876472
No duplicates:

// 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));
}
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

ID: 13878405
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

## Featured Post

Question has a verified solution.

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

Sometimes you know that one object has a specific child in it, but you can't find the child. This happened to me when I was trying to code some actionScript to make a toolbar work with its embedded buttons.  My partner had created the toolbar usi…
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,…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
The goal of the tutorial is to teach the user how to how to record live broadcast.
###### Suggested Courses
Course of the Month18 days, 6 hours left to enroll