Solved

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

Posted on 2006-07-15
9
357 Views
Last Modified: 2008-01-09
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.



0
Comment
Question by:sp400
  • 5
  • 4
9 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 17113985
// 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>
0
 

Author Comment

by:sp400
ID: 17114018
mplungjan,

I need several (as 4 or so) not-repeat random images
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 17114047
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>
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:sp400
ID: 17114190
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>
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 17114211
Sorry.

  var rnd = Math.round(Math.random()*(myImages.length-1));
0
 

Author Comment

by:sp400
ID: 17114261
sorry,
this line where? or instead of?
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 125 total points
ID: 17114684
Ohh, I was rushed out of the door..


<!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() {
  var rnd = Math.round(Math.random()*(myImages.length-1));
  return myImages[rnd]
}
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>
0
 

Author Comment

by:sp400
ID: 17114771
Now works. Thanks.

maybe you can help me in other open question:
http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21903759.html

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 17115248
I would think you can find such a thing on the net as a drop in windowing script.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Hide cell in a table 2 27
Jquery 2 23
Html value of radio 14 29
Why is initMap returning "not a function" error. 3 30
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question