File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Random, random Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Random, random" Watch "Random, random" New topic

Random, random

Gjorgi Var
Ranch Hand

Joined: Feb 24, 2004
Posts: 85
How would you force a random selection with Math.random from a section of range of possible values using only loops and Math.random()... you should be also able to exclude chosen values out of selection from that section. Say range is 1-100, you would like to be able to choose randomly from 10-70 range and prevent certain numbers from 10-70 to be selected... yeap, the begginer... am not asking for the code...
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Well, to get an integer from 0 to 60, you can use Random.nextInt() and the modulo (%) operator to get only the remainder of dividing by 60. Then you can add 10 to get a number from 10 to 70. Then you could evaluate this in a loop; after each evaluation, check to see if the random value is one of the forbidden ones; if so, restart the loop; otherwise, you've got a good value, so break out of the loop.

[Jess in Action][AskingGoodQuestions]
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Also, pay attention to the max and min values. Ernest's algorithm above will give you numbers as low as 10 and as high as 69, but never 70. Maybe that's what you really needed, or maybe not. To get a range that includes both 10 and 70, you'd need 61 different possible values, not 60.
Also, if you use the Random class, you don't even need the modulo (%) operator - you can just use nextInt(int) instead. (See API for details.) If you must use Math.random() rather than the Random class, then you can just multiply the random() by 60 or 61 or whatever you need, then cast the result to int.

"I'm not back." - Bill Harding, Twister
Gjorgi Var
Ranch Hand

Joined: Feb 24, 2004
Posts: 85
Nothing like a little push from the sheriffs...
I want to use the Math.random rather than Random.nextInt. If I followed Ernest's advice right, I would always get 10 as a result:
As far as just multiplying Math.random by the ceiling (60), I would also get values below desired floor (below 10)...
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

Ernest's methos used Random.nextInt(), which returns an integer between 0 and (i don't remember exactly how large, but pretty big). hence the modulo operator to trim it down to the range you want.
Math.random() returns a double greater or equal to 0.0 and less than 1. So yes, Ernests method won't work with Math.random.(), but you shouldn't expect it to.
what you will need to do is MULTIPLY your double by the number of values in your range, which is 61 (10-70 inclusive). then, you need to add 10 to shift the values up to where you want them.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Gjorgi Var
Ranch Hand

Joined: Feb 24, 2004
Posts: 85
Originally posted by fred rosenberger:
what you will need to do is MULTIPLY your double by the number of values in your range, which is 61 (10-70 inclusive). then, you need to add 10 to shift the values up to where you want them.

After coding this accordingly, I got what I needed... A big "thank you" from a begginer lost in fog... any literature recomended on problems like this?
sever oon
Ranch Hand

Joined: Feb 08, 2004
Posts: 268
Well, you have some interesting requirements. If you want to choose a random int between x (inclusive) and y (exclusive), use the following method:

If you call the above method with min=5 and max=15, you'll get back x where 5 <= x < 15.
Now you also have the requirement that you want some values of x where min <= x < max to be prohibited as well. So you might want a random number between 0 and 100, but it can't be between 70 and 80. (Always assume, for the sake of simplicity, that I'm talking inclusive-exclusive ranges, so "between 0 and 100" means you could get back 0, but you could not get back 100.)
In that case, you need a method into which you can pass ranges of values, like 0 to 70, 81 to 100. When I look at this problem I think about mapping what you're really asking for is a random number between 0 and 90, then when you get that number back if it's >= 70, you want to add 11 to it.
The way I would go about solving this problem is to develop a Range class that represents a range of values from some min <= x < some max. You can add all sorts of methods to it so you can compare Ranges, merge them, expand and contract them, etc. And you can write a random number generator backed by the JDK's that works with them to develop evenly distributed random numbers over whatever series of Ranges you can instantiate.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Gjorgi Var:

any literature recomended on problems like this?

I think some books on mathematics might help. In particular, you should look for books on algebra and discrete math. Sorry, I don't have any particular titles in mind, but hopefully the categories will help.

Java API Documentation
The Java Tutorial
I agree. Here's the link:
subject: Random, random
jQuery in Action, 3rd edition