Solved

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

Posted on 2006-07-15
9
354 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now