Link to home
Start Free TrialLog in
Avatar of sp400
sp400Flag for Spain

asked on

non repeating random numbers javascript (select 4 images from N)

I've about 60 images and I like show 4 random not-repeat when page start, maybe nice if these are repeated each two minutes or so.

My main dificulties are in be sure I select not repeated numbers.

I would like have the list of images in an external file (images.js or images.txt) for easy maintain. the list can be in the format
xxxxx.jpg
yyyyyyy,png
zzz.gif
ww.jpg

or xxxxx.jpg,yyyyyyy,png,zzz.gif,ww.jpg

then I can have a table with 4 cells and put one image in each cell or so on.

Of course I can today having 60 images and next month 64, then the numbers can be calculated using array.length or so on.

Thanks in advance.



Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

// JS File:
myImages = new Array(
"img1.gif",
"img2.jpg",
.
.
.
imgx.gif")
function getRndImg() {
  return myImages[Math.round(Math.random()*myImages.length)]
}
// end of js file


page.html:
<html>
<head>
<script src="images.js"></script>
</head>
<body>
.
.
.
<script>
document.write('img src="'+getRndImg()+'">')
</script>
Avatar of sp400

ASKER

mplungjan,

I need several (as 4 or so) not-repeat random images
Ahh.

Sorry.

Here

<script>
myImages = new Array(
"img0.jpg",
"img1.jpg",
"img2.jpg",
"img3.jpg",
"img4.jpg",
"img5.jpg",
"img6.jpg",
"img7.jpg",
"img8.jpg",
"img9.jpg",
"img10.jpg",
"img11.jpg",
"img12.jpg",
"img13.jpg",
"img14.jpg",
"img15.jpg",
"img16.jpg",
"img17.jpg",
"img18.jpg",
"img19.jpg",
"img20.jpg",
"img21.jpg",
"img22.jpg",
"img23.jpg",
"img24.jpg",
"img25.jpg",
"img26.jpg",
"img27.jpg",
"img28.jpg",
"img29.jpg",
"imgx.gif")

function getRndImg() {
  return myImages[Math.round(Math.random()*myImages.length)]
}
function makeArray() {
  var list = "";
  var arr = new Array();
  cnt = 0;
  while (cnt!=nofRandom) {
    var img = getRndImg();
    if (list.indexOf(img)==-1) {
      arr[cnt++]=img
      list+=img;
    }
  }
  return arr;
}
nofRandom = 4;
// end of js file


.
.


<script>
arr = makeArray();
for (i=0;i<arr.length;i++) document.write('<br><img src="'+arr[i]+'">')
</script>
Avatar of sp400

ASKER

better, but after some repetition one item returns undefined
please retest.
here is the code. I can send to you the seven test images but don't fund any upload here.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head>
<title>Random images EE</title>
</head>
<body>
<script type="text/javascript">
myImages = new Array(
"star_1077.gif",
"questionmark.gif",
"icon_eek.gif",
"glass_blue.gif",
"DEVIL.GIF",
"GREMGR3.GIF",
"help_836.gif"
)
function getRndImg() {
  return myImages[Math.round(Math.random()*myImages.length)]
}
function makeArray() {
  var list = "";
  var arr = new Array();
  cnt = 0;
  while (cnt!=nofRandom) {
    var img = getRndImg();
    if (list.indexOf(img)==-1) {
      arr[cnt++]=img
      list+=img;
    }
  }
  return arr;
}
nofRandom = 4;
</script>
<h3>random images EE</h3>
<script>
arr = makeArray();
for (i=0;i<arr.length;i++)
    {document.write('<img src="' + arr[i] + '">&nbsp;' + arr[i]+ '<br><br>')}
</script>
</body></html>
Sorry.

  var rnd = Math.round(Math.random()*(myImages.length-1));
Avatar of sp400

ASKER

sorry,
this line where? or instead of?
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sp400

ASKER

Now works. Thanks.

maybe you can help me in other open question:
https://www.experts-exchange.com/questions/21903759/div-as-window-into-the-main-document.html

I would think you can find such a thing on the net as a drop in windowing script.