Solved

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

Posted on 2006-07-15
9
356 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Trying to redirect after event in javascript 3 24
Browser Chrome downloads colored link 5 41
Html5 Index on a table 7 29
Javascript Form Change 5 23
Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

831 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