Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

if (Math.random() > 0.5)

Posted on 2009-04-07
2
Medium Priority
?
1,959 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
[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
2 Comments
 
LVL 43

Expert Comment

by:David S.
ID: 24094473
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 500 total points
ID: 24094894
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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'…
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…

618 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