Page 1 of 1

## Random number within a given range Rate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=51264&amp;s=4056b0e1d6724a177d35ef8cef8040d2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 no2pencil

• Professor Snuggly Pants

Reputation: 6219
• Posts: 29,807
• Joined: 10-May 07

Posted 06 May 2008 - 07:04 PM

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.

```<html>
<title>Javascript Random Numbers</title>
<body>
<script language=Javascript>
<!--
var rand_no = Math.floor((10-4)*Math.random()) + 5;
-->
</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

```<html>
<title>Javascript Random Numbers</title>
<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.

Is This A Good Question/Topic? 0

## Replies To: Random number within a given range

### #2 mocker

• D.I.C Regular

Reputation: 51
• Posts: 466
• Joined: 14-October 07

Posted 07 May 2008 - 09:35 AM

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

```function newLink(){
var random_num = (Math.floor((Math.random()*lmax)+1));
d.innerHTML = newhtml;
};

```

### #3 bredmond812

Reputation: 0
• Posts: 2
• Joined: 19-August 14

Posted 19 August 2014 - 10:40 PM

Can that "Random number within a given range" be used elsewhere to compute a math equation?

My final plan is this: I am studying accounting and I want to practice the same question over and over again but using different numbers. I also want the computer to be able to generate the answer so that I can check my calculations. If I can replace the text with some of my own, like "Sales in 2014" instead of "Here is a random number between 0 and 50:" and if I do this a few more places with other important figures such as "Net Operating Profit After Taxes in 2014", Then I can have a question that says: What is the RNOA in 2014? (which is Sales/NOPAT) and then maybe the answer can be revealed after I have tried to calculate the answer.

I have other features I want to do too, but i will do it little by little. If you can help point me in the right direction, i will work with cutting and pasting to try and figure out how to get it working the way I want it.

This post has been edited by macosxnerd101: 19 August 2014 - 10:43 PM
Reason for edit:: Removed tutorial quote

### #4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11785
• Posts: 44,289
• Joined: 27-December 08

Posted 19 August 2014 - 10:46 PM

Welcome to Dream.in.Code!

Yes- of course random number generation can be used elsewhere, such as in the application you described. Please be advised that we are not a copy/paste forum. We encourage learning and pedagogy rather than "copy/pasting together a solution." Also, tutorials are not the best place to ask for help on extensions and semi-unrelated projects. I advise you to post in the standard help forums. Please make sure to include your good faith efforts, and our members will be happy to help!

### #5 bredmond812

Reputation: 0
• Posts: 2
• Joined: 19-August 14

Posted 19 August 2014 - 10:50 PM