School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,355 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,948 people online right now. Registration is fast and FREE... Join Now!




Random number within a given range

 
Reply to this topicStart new topic

> Random number within a given range

no2pencil
Group Icon



post 6 May, 2008 - 06:04 PM
Post #1


Random number within a given range

Here are the steps in order to generate random numbers from a given range.
1st, we get a return value from the random() function. This value will be between 0.0 & 1.0.
We then multiply this value with the difference of the maximum value, & one less than the minumum value in our given range.
After that, we use the floor() function, to convert it into an integer.
Lastly, add the minimum value of our given range, to the number we came up with from above.

CODE

<html>
<head>
<title>JavaScript Random Numbers</title>
</head>
<body>
<script language=JavaScript>
<!--
var rand_no = Math.floor((10-4)*Math.random()) + 5;
alert(rand_no);
-->
</script>
</body></html>


The code above generates a random number between 5 and 10. With 5 being our minimum, & 10 being our maximum.

Random number within an array

CODE

<html>
<head>
<title>JavaScript Random Numbers</title>
</head>
<body>
<script language="JavaScript">
<!--
movie = new Array
movie[1]="Casablanca"
movie[2]="The Wizard of Oz"
movie[3]="The Dirty Dozen"
movie[4]="Who Framed Roger Rabbit?"
movie[5]="The Five Heartbeats"
movie[6]="Battleground"
movie[7]="The Life and Times of Hank Greenberg"
movie[8]="The Battle for Heavy Water"
movie[9]="My Blue Heaven"

var rand_no = Math.floor((9-1)*Math.random()) + 1;

document.write(movie[rand_no]);
-->
</script>
</body></html>


The code example above generates a random number between 1 and 9. Again, with 1 being our minimum, & 9 being our maximum.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

mocker
Group Icon



post 7 May, 2008 - 08:35 AM
Post #2
Just to add to your last example.. if you want to get a random element of the array, it usually is not a good idea to hardcode how many elements are in that array. I use the following code to get a random element of an array links[] and write it to a div

CODE

function newLink(){
var lmax = links.length;            
var random_num = (Math.floor((Math.random()*lmax)+1));
var d = document.getElementById('rlink');        
var newhtml = "<a href='"+links[random_num]+"'>"+links[random_num]+"</a>";
d.innerHTML = newhtml;
};
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/7/09 07:29PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month