Solved

if (Math.random() > 0.5)

Posted on 2009-04-07
2
1,442 Views
Last Modified: 2012-06-27
Hello,

I have this small script that change on refresh. I want to make sure that it displays 50 / 50% to the visitors.

is if (Math.random() > 0.5) than correctly chosen?
is there a better way to do this?
<html > 

<head> 

<title>Random Images</title> 

<script type="text/javascript"> 

//<![CDATA[ 

var images1="image_1.jpg"; 

var imglink1="http://www.test1.com";

var alt1="test1";
 

var images2="image_2.jpg"; 

var imglink2="http://www.test2.com";

var alt2="test2";
 

function randomImage() { 

if (Math.random() > 0.5) {

document.getElementById("random").src=images1; 

document.getElementById("link").href=imglink1; 

document.getElementById("random").alt=alt1; 

}

else {

document.getElementById("random").src=images2; 

document.getElementById("link").href=imglink2; 

document.getElementById("random").alt=alt2; 

}

} 

onload=randomImage; 

//]]> 

</script> 

</head> 

<body > 

<div id="container"> 

<a href="" id="link"><img id="random" src="" alt=""/></a> 

</div> 

</body> 

</html>

Open in new window

0
Comment
Question by:mvanthof
2 Comments
 
LVL 42

Expert Comment

by:David S.
Comment Utility
I'd use ">=" instead of just ">" because the range is between 0 (inclusive) and 1 (exclusive).

No, there is no better way to do it.
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 125 total points
Comment Utility
However I would do this instead:

<html > 

<head> 

<title>Random Images</title> 

<script type="text/javascript"> 

//<![CDATA[

function crtImage(imgUrl,href,title) {

  this.imgUrl = imgUrl;

  this.href=href;

  this.title=title

} 

var myImages = new Array(

  new crtImage("image_1.jpg","http://www.test1.com","test1"),

  new crtImage("image_2.jpg","http://www.test2.com","test2")

); // note the lack of comma on the last item  

 

function randomImage() { 

  var rnd = parseInt(Math.random()*myImages.length);

  var imgObj = myImages[rnd];

  document.getElementById("random").src=imgObj.imgUrl; 

  document.getElementById("link").href=imgObj.href; 

  document.getElementById("link").title=imgObj.title; 

} 

window.onload=randomImage; 

//]]> 

</script> 

</head> 

<body > 

<div id="container"> 

<a href="" id="link"><img id="random" src="" alt=""/></a> 

</div> 

</body> 

</html>

Open in new window

0

Featured Post

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

Join & Write a Comment

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…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

772 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

11 Experts available now in Live!

Get 1:1 Help Now