Solved

How to choose RANDOM between 4 specified document.write ?

Posted on 2009-07-10
4
503 Views
Last Modified: 2013-11-11
Hi,
I want the script to choose random between 4 specific choices.

Using this code it does that, but somehow it NEVER goes to pick choice number 2.  Or, it shows version 1, 3 and 4 - but never number 2. It in stead shows nothing every once in a while.

Where's the error?

<script type="text/javascript">
var random_number = Math.random();
if (random_number < 0.25){
document.write("Hello 1!");
}
else if (random_number >0.25  <0.5){
document.write("Hello 2!");
}
else if (random_number >0.5  <0.75){
document.write("Hello 3!");
}
else if(random_number >0.75  <1){
document.write("Hello 4!");
}
</script>
<script type="text/javascript">

var random_number = Math.random();

if (random_number < 0.25){

document.write("Hello 1!");

}

else if (random_number >0.25  <0.5){

document.write("Hello 2!");

}

else if (random_number >0.5  <0.75){

document.write("Hello 3!");

}

else if(random_number >0.75  <1){

document.write("Hello 4!");

}

</script>

Open in new window

0
Comment
Question by:Maxrespons
  • 2
  • 2
4 Comments
 
LVL 41

Accepted Solution

by:
HonorGod earned 125 total points
ID: 24822069
var pick = Math.floor( Math.random() * 4 )

Result will be an integer from 0 to 3, which could/should be used to index an array:

var message = [ 'Hello 1', 'Hello 2', 'Hello 3', 'Hello 4' ]

document.write( message[ pick ] );
0
 

Author Comment

by:Maxrespons
ID: 24822210
Thank you!
I used my code but with your Math.floor( Math.random() * 4 )

and made these changes:
if (random_number == 0){

up to

if (random_number == 3){

That made it all work perfect ;-)
0
 

Author Closing Comment

by:Maxrespons
ID: 31602016
Thanks for helping! - Combining your code and my code made it work like I needed. :-)
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24827991
Excellent.  Glad to be able to help.

Thanks for the grade & points.

Good luck & have a great day.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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…

707 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

12 Experts available now in Live!

Get 1:1 Help Now