lukegriffiths110
asked on
duplicated numbers
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(Ra nd0))+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(Ra nd1))+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(Ra nd2))+1))) {
if ((substring(Rand2, x, 1)) eq rnd) {
rnd = "";
}
x = Number(x)+1;
}
if (rnd ne "") {
Rand2 = Rand2 add rnd;
i = Number(i)+1;
}
}
Rand0="";
i = 1;
while (Number(i)<>3) {
rnd = random(5);
x = 1;
while (Number(x)<>Number((Number
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
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
if ((substring(Rand2, x, 1)) eq rnd) {
rnd = "";
}
x = Number(x)+1;
}
if (rnd ne "") {
Rand2 = Rand2 add rnd;
i = Number(i)+1;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
// 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];