Solved

How to choose RANDOM between 4 specified document.write ?

Posted on 2009-07-10
4
504 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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 how to dynamically set the form action using jQuery.
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)

920 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

15 Experts available now in Live!

Get 1:1 Help Now