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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

3 Random Numbers

Status
Not open for further replies.

cluksha

Programmer
Mar 11, 2004
19
US
I am trying to pick 3 random numbers from an array length. (I have 5 images that I need to show 3 of each time a page loads - but none can be the same.)

So I need to pick 3 random images from a list of 5 and ther ecan be no duplicates - like I have already done in this post :)

Anyway I grabbed a script from somewhere I can't remember. and modified it slightly - but I keep getting "undefined" returned as a result.

If you try this code you will see what I mean...

//creates new array filled w/ images and new array filled w/ URL's
images = new Array("agility","strength","balance","power","speed")
//So you can add and subtract images as you choose w/o worry
imgCt = images.length


<!-- Begin
num= new Array(imgCt);
for (i in images) {

num=Math.round(imgCt*Math.random()+1);
if (num[1]==num[0]) {
while (num[1]==num[0]) {
num[1]=Math.round(imgCt*Math.random()+1);
}
}
if (num[2]==num[1] || num[2]==num[0]) {
while (num[2]==num[1] || num[2]==num[0]) {
num[2]=Math.round(imgCt*Math.random()+1);
}
}
if (num[3]==num[2] || num[3]==num[1] || num[3]==num[0]) {
while (num[3]==num[2] || num[3]==num[1] || num[3]==num[0]) {
num[3]=Math.round(imgCt*Math.random()+1);
}
}

document.write(images[num],"-",i,",");
//This is here for testing purposes only
}

Then I want to put this in the page -
document.write ("<a href='programs/'" + eval(images[num[0]]) + "><img src='img/" + eval(images[num[0]]) + ".jpg' width=750 height='122' border='0' alt=''></a><br>");


Can anyone help me find the solution here? ( I will be away fro a few days at this point but at least I have it out there for you to see.

Thanks so much,
Chris

Chris Luksha
Echo Web Services
Making Your Website Resound
 
This:

Math.round(imgCt*Math.random()+1);

... generates number between 1 and 6. You need between 0 and 4, right?

------
[small]select stuff(stuff(replicate('<P> <B> ', 14), 109, 0, '<.'), 112, 0, '/')[/small]
[banghead]
 
That line actually generates number from 0-4 as there are 5 items in my imgCt var. The problem is that occassionally it generates an "undefined" when pulling it's relative item from the images array.

So if the value is undefined - then it will not show an image.

Thanks
Chris

Chris Luksha
Echo Web Services
Making Your Website Resound
 
As said, the formula for generating num before testing is not correct. This is how you can do it more neatly and easier to read.
[tt]
for (var i=0;i<num.length;i++) {
do {
var b=true;
num=Math.floor(imgCt*Math.random());
for (var j=0;j<i;j++) {
if (num[j]==num) { b=false; break;}
}
} while (!b)
}
alert(num); //just to show
[/tt]
 
It's generating the "undefined" because you're rounding the random number, then adding one. That could result in an index number 2 greater than the length of the array.

Try the following code. It should be flexible enough for your requirements.

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

var pics = new Array(), numpics = 4;
var img = new Array("agility","strength","balance","power","speed");
var imglength = img.length;

for (var pi=0;pi<numpics;pi++)
  {
  do
    {
    var duplicate = false;
    pics[pi] = Math.floor(Math.random() * imglength);
    for (var ni=0;ni<pi;ni++)
      {
      if (pics[pi] == pics[ni])
        {
        duplicate = true;
        }
      }
    }
  while (duplicate == true);
  
  //display the image name chosen randomly.
  alert(img[pics[pi]]);
  }
</script>

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top