Click to See Complete Forum and Search --> : Random image order help.


firestarter
04-14-2003, 07:24 AM
Hi,
I use JavaScript, but only at a very basic level, leading me to find a problem that I do not seem to be able to solve. So if any body could help I would be very grateful.

On the side of the page I would like to have 5 individual images horizontally in a row, each one having its own individual hyperlink. However I would like the order of these to be random so that no one image could be seen as having a "better position". I know how to make every image random, but if I were to do that I would possibly end up with the same image twice (out of the five) for they would be independent of each other. So does any body have any ideas on how to make the set as a whole random, not just the individual images, whilst ideal keeping each images individual hyperlink!

I hope that made sense. Many thanks if any body could point me in the right direction.

Kate.

khalidali63
04-14-2003, 07:33 AM
Originally posted by firestarter
Hi,
I use JavaScript, but only at a very but if I were to do that I would possibly end up with the same image twice (out of the five) .....

Kate.

This is what I'd suggest for you.
write a function that give you a unique randome number within the range of
1 to 5

that way you will not have an image twice.

I or some one else may be able to guide you inwriting such function

Cheers

Khalid

firestarter
04-14-2003, 07:49 AM
Hi khalidali63,
Yeah I think that is what I am strugling on, the random number bit is fine, but I am not sure how to go about making sure that all five random numbers are unique.

Thanks,
Kate.

khalidali63
04-14-2003, 08:16 AM
Here is the logic,try to implements this.
1. create an empty array
2. run a while loop which will run until array length is not equal to 4 ( since array index starts from 0 the 4 the element is 5th in reality.
3. in the while loop get a randome number between 1 to 5
4. run a for loop to the lcurrent length of the array
5. validate that the new number generated is not in the array
6. if not then add into the array else just skip

This way it will loop through until it has 5 unique numbers in the array.

Hope this points in right direction

Cheers

Khalid