# Math.random

Argm Mastoi

Ranch Hand

Posts: 35

posted 15 years ago

Thanx for your reply i learned quite alot but the formula u told is like for a range of 10 numbers from 30-40:

Math.random()*10+30

but it is not working in that way.

only the formula i mentioned before is working properly and i don't know what is the logic behind that formula

[This message has been edited by Argm Mastoi (edited October 18, 2001).]

Math.random()*10+30

but it is not working in that way.

only the formula i mentioned before is working properly and i don't know what is the logic behind that formula

[This message has been edited by Argm Mastoi (edited October 18, 2001).]

Dale DeMott

Ranch Hand

Posts: 515

posted 15 years ago

It works like this.. the Math.random() method creates a number between 0 and 1. so it will randomly create a floating point number. You then can create any number from that by adjusting the range and starting point. The range is what you multiply by and the starting point is what you add on after this. Example

I want to create a number from 1 to 10

I would do the following

Math.random()*10

so I get a .31 for my Math.random part. I do the multiply and get 3.1

So my number is now 3.

Next I decide... no infact I need a number from 30 to 40. My range is 10 and my starting point is 30. So I then start again

Lets say again I get .31

I multiply by 10 and get 3.1 .. which infact is cut off to be 3

then I add my base of 30.

I get 33.

Get it!?

So in your case you have the following

---------------------------------

Math.random()*(1000-500)+500

the first part creates a number between 0 and 1

lets again say .31

The 1000-500 is calculated first to get 500 so we know your range is 500. We take your number and multiply it by 500. .31 * 500 is 155. Then we add 500 to it and get 655. It works great. So your formulate says this...

Math.random()*(endingRangeNumber-StartingRangeNumber)+StartingPoint

Or if you want to simplify it

Math.random()*(rangeOfNumbersInYourSet) + startingPoint

So if you wanted a number between 300 and 350, you would have this

(Math.random() * 50) + 300

Hope this helps.

Dale

------------------

By failing to prepare, you are preparing to fail.

Benjamin Franklin (1706 - 1790)

[This message has been edited by Dale DeMott (edited October 18, 2001).]

I want to create a number from 1 to 10

I would do the following

Math.random()*10

so I get a .31 for my Math.random part. I do the multiply and get 3.1

So my number is now 3.

Next I decide... no infact I need a number from 30 to 40. My range is 10 and my starting point is 30. So I then start again

Lets say again I get .31

I multiply by 10 and get 3.1 .. which infact is cut off to be 3

then I add my base of 30.

I get 33.

Get it!?

So in your case you have the following

---------------------------------

Math.random()*(1000-500)+500

the first part creates a number between 0 and 1

lets again say .31

The 1000-500 is calculated first to get 500 so we know your range is 500. We take your number and multiply it by 500. .31 * 500 is 155. Then we add 500 to it and get 655. It works great. So your formulate says this...

Math.random()*(endingRangeNumber-StartingRangeNumber)+StartingPoint

Or if you want to simplify it

Math.random()*(rangeOfNumbersInYourSet) + startingPoint

So if you wanted a number between 300 and 350, you would have this

(Math.random() * 50) + 300

Hope this helps.

Dale

------------------

By failing to prepare, you are preparing to fail.

Benjamin Franklin (1706 - 1790)

[This message has been edited by Dale DeMott (edited October 18, 2001).]

By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)

Argm Mastoi

Ranch Hand

Posts: 35

Margaret Tan

Greenhorn

Posts: 20

Margaret Tan

Greenhorn

Posts: 20

Michael Bruesch

Ranch Hand

Posts: 158

posted 15 years ago

You are correct Margaret, the Math.random () function returns a double value greater than or equal to 0.0 and strictly less than 1.0. and if you multiply that number by a range number, you'll get random numbers less than the range max, but never exactly the range max. Now if you cast the random numbers to ints like so:

(int) (Math.random () * rangeMax);

then you'll get a list of integers between 0 and rangeMax. If you want to include the rangeMax, you'll have to add one to it, then multiply by Math.random ().

------------------

Michael J Bruesch

Codito, ergo sum...

http://www.geocities.com/mjbruesch

(int) (Math.random () * rangeMax);

then you'll get a list of integers between 0 and rangeMax. If you want to include the rangeMax, you'll have to add one to it, then multiply by Math.random ().

------------------

Michael J Bruesch

Codito, ergo sum...

*I code, therefore I am.*http://www.geocities.com/mjbruesch

Michael J Bruesch<br /><i>I code, therefore I am.</i>

Margaret Tan

Greenhorn

Posts: 20

Margaret Tan

Greenhorn

Posts: 20

Michael Bruesch

Ranch Hand

Posts: 158

posted 15 years ago

if you need the numbers in your range to be integers, then the following should suffice:

------------------

Michael J Bruesch

Codito, ergo sum...

http://www.geocities.com/mjbruesch

------------------

Michael J Bruesch

Codito, ergo sum...

*I code, therefore I am.*http://www.geocities.com/mjbruesch

Michael J Bruesch<br /><i>I code, therefore I am.</i>

Margaret Tan

Greenhorn

Posts: 20

posted 15 years ago

uh uh , doesn't work Mike, here is the code and the output

class TestRandom{

public static void main(String args[]){

int num, i=0;

int rangeMin=30;

int rangeMax=40;

do{

//generates random number starting from rangeMin to rangeMax

num = (int)(Math.random()*(rangeMax+1)+rangeMin);

System.out.println(num);

}while(num!=40);

}

}

output from DOS prompt.. see how it generate a number more than 40?

35

48

59

53

48

33

40

class TestRandom{

public static void main(String args[]){

int num, i=0;

int rangeMin=30;

int rangeMax=40;

do{

//generates random number starting from rangeMin to rangeMax

num = (int)(Math.random()*(rangeMax+1)+rangeMin);

System.out.println(num);

}while(num!=40);

}

}

output from DOS prompt.. see how it generate a number more than 40?

35

48

59

53

48

33

40

Michael Bruesch

Ranch Hand

Posts: 158

posted 15 years ago

Whooops, sorry Margaret, had the close parenthesis in the wrong place, should be:

Sorry!

------------------

Michael J Bruesch

Codito, ergo sum...

http://www.geocities.com/mjbruesch

Sorry!

------------------

Michael J Bruesch

Codito, ergo sum...

*I code, therefore I am.*http://www.geocities.com/mjbruesch

Michael J Bruesch<br /><i>I code, therefore I am.</i>

Michael Bruesch

Ranch Hand

Posts: 158

posted 15 years ago

Alright, I'm all confused. I'm sorry, the variable name shouldn't be rangeMax, it should be just range. In your case, the range of numbers is 10, ie. from 30 to 40. So substitue range for rangeMax and you're homefree. I compiled and ran this code to make sure it's correct:

------------------

Michael J Bruesch

Codito, ergo sum...

http://www.geocities.com/mjbruesch

------------------

Michael J Bruesch

Codito, ergo sum...

*I code, therefore I am.*http://www.geocities.com/mjbruesch

Michael J Bruesch<br /><i>I code, therefore I am.</i>

Michael Bruesch

Ranch Hand

Posts: 158

Argm Mastoi

Ranch Hand

Posts: 35

posted 15 years ago

hi Margaret,

If you've closely looked over the code, provided by Dale, you can easily understand that in the formula, there's an starting point(where to start?) and inspite of the ending point the range of number is given(means upto what number of numbers it has to generate) so its you to tell the range whether 10 or 11.

for numbers 30-40:

(int)((Math.random()*11)+30)

I hope this formula will work out.

Argm

If you've closely looked over the code, provided by Dale, you can easily understand that in the formula, there's an starting point(where to start?) and inspite of the ending point the range of number is given(means upto what number of numbers it has to generate) so its you to tell the range whether 10 or 11.

for numbers 30-40:

(int)((Math.random()*11)+30)

I hope this formula will work out.

Argm

Michael Bruesch

Ranch Hand

Posts: 158

posted 15 years ago

Don't know if any changes have been made to Math.random() recently but I have read advice from one author to use the java.util.Random class instead of Math.random() as the latter has a flaw in its algorithm.

------------------

Junilu Lacar

Sun Certified Programmer for the Javaï¿½ 2 Platform

------------------

Junilu Lacar

Sun Certified Programmer for the Javaï¿½ 2 Platform

Junilu - [How to Ask Questions] [How to Answer Questions]

Dale DeMott

Ranch Hand

Posts: 515

posted 15 years ago

At the risk of being redundant I wanted to clarify what I was saying so you could understand it a bit more. Again the formula is this

Math.random()*(rangeOfNumbersInYourSet) + startingPoint

If you wanted to create a list of numbers from 30 to 40 (including the ending points 30 and 40), there are 11 numbers in the list so your range would have 11 in it. You have to remember when you are subtracting numbers, you are actually counting the spaces between the numbers. Not the numbers themselves. If you want to count the numbers, you always need to add 1. Example - If I want a range of numbers from 0 to 4 (including 4), I need to tell the system 5. Reason being is that there are 5 numbers in my set. 0, 1, 2, 3, 4. I should have altered my formula to state the following

Math.random()*(rangeOfNumbersInYourSet + 1) + startingPoint

This usually appropriate since most people assume the range will include the last element in the list. So again if I wanted a range of numbers from 300 to 350, (including 350) it would look like this.

Math.random()*(350 - 300 + 1) + 300

Regards,

Dale DeMott

------------------

By failing to prepare, you are preparing to fail.

Benjamin Franklin (1706 - 1790)

Math.random()*(rangeOfNumbersInYourSet) + startingPoint

If you wanted to create a list of numbers from 30 to 40 (including the ending points 30 and 40), there are 11 numbers in the list so your range would have 11 in it. You have to remember when you are subtracting numbers, you are actually counting the spaces between the numbers. Not the numbers themselves. If you want to count the numbers, you always need to add 1. Example - If I want a range of numbers from 0 to 4 (including 4), I need to tell the system 5. Reason being is that there are 5 numbers in my set. 0, 1, 2, 3, 4. I should have altered my formula to state the following

Math.random()*(rangeOfNumbersInYourSet + 1) + startingPoint

This usually appropriate since most people assume the range will include the last element in the list. So again if I wanted a range of numbers from 300 to 350, (including 350) it would look like this.

Math.random()*(350 - 300 + 1) + 300

Regards,

Dale DeMott

------------------

By failing to prepare, you are preparing to fail.

Benjamin Franklin (1706 - 1790)

By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)