Solved

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

Posted on 2006-07-15
9
359 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

740 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