Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

picking 6 unique #s from 1-14 2

Status
Not open for further replies.

DaveC426913

Programmer
Joined
Jul 28, 2003
Messages
274
Location
CA
Rusty on JavaScript.

I have a list of 14 text strings of which I want to randomly pick 6 unique ones.

for (i=1;i<=6;i++){
tmp = Math.round(Math.random()*14)+1;
for (j=1;j<i;j++){
if (tmp == col[j]){
i++;
}
}
col=tmp;
}

I've got it wrong: if it fails uniqueness, I don't want to increment i, I just want it to try another random #.
 
[Edit: That Math.round() should be Math.floor() ]
 
OK, this works, but it's ugly:

for (i=1;i<=6;i++){
tmp = Math.floor(Math.random()*14)+1;
for (j=1;j<i;j++){
if (tmp == col[j]){
tmp = Math.floor(Math.random()*14)+1;
j=0;
}
}
col=tmp;
}
 
Code:
<script type="text/JavaScript" language="JavaScript">

numStr = ""
for (x=1; x<=6; x++){
  do{
    tmp = Math.floor(Math.random()*14)+1;
    tmp = tmp + ","
  } while (numStr.indexOf(tmp) != -1)
  numStr = numStr + tmp
}
alert(numStr)


</script>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
This is kinda sexy - runs in only one loop. It'd be 3 lines if javascript had an inherent swap function, or if you could pop any element out of an array (not just the last):

Code:
<script type="text/javascript">

var arr = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen"]

var result = "";
var str = "";
var rnd = 0;

for (i = 0; i < 6; i++) {
   [gray]//generate the random number[/gray]
   rnd = Math.floor(Math.random() * arr.length);
   [gray]//swap random number with last element[/gray]
   str = arr[rnd];
   arr[rnd] = arr[arr.length - 1];
   arr[arr.length - 1] = str;
   [gray]//pop the last element[/gray]
   result += arr.pop() + "\n";
}

alert(result);

</script>

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Good one, kaht. I'm going to have to remember that algorithm in the future. :)#

Lee
 
thanks! [thumbsup2]

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Yeah, that last one's slick! Thanks y'all!
 
Got the loop condensed even more to only 2 lines (had do look up some array functions today and came across splice, don't even have to use pop this way):

Code:
<script type="text/javascript">

var arr = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen"]

var result = "";
var str = "";
var rnd = 0;

for (i = 0; i < 6; i++) {
   //generate the random number
   rnd = Math.floor(Math.random() * arr.length);
   result += arr.splice(rnd, 1) + "\n";
}

alert(result);

</script>

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top