This discussion is archived
1 2 Previous Next 22 Replies Latest reply: Dec 19, 2009 10:40 PM by 843789

# Random Number Selection Help

Currently Being Moderated
Hello All,

I have a question about the random number generator. I am trying to create a random process that will select between two numbers, which happen to be doubles, either 0.5 or -0.5. Yes, I want the random the calculation to either add .5 or subtract .5. So far I can only get the random number to add by .5 and never subtract by -.5. Help please!

class RandomTrialSets
{
public static void main (String[]args)
{
Random myRandom = new Random();

double randomOne = myRandom.nextDouble() * 0 + .5;
double randomTrials;

for (randomTrials = 5; randomTrials !=10; randomTrials += RandomOne)
{
System.out.println("it took this many times to complete the trial" + randomTrials);

Edited by: s_cheplick12 on Dec 14, 2009 8:19 PM
• ###### 1. Re: Random Number Selection Help
Currently Being Moderated
heck, I'd use a myRandom.nextBoolean() and mult 0.5 by 1 vs -1 if true vs false, then add.
• ###### 2. Re: Random Number Selection Help
Currently Being Moderated
I can't work out whether you want
double randomOne = myRandom.nextDouble() - 0.5;
or
double randomOne = myRandom.nextInt(2) - 0.5;
• ###### 3. Re: Random Number Selection Help
Currently Being Moderated
I will try out both of your suggestions and write back in a little bit if they work. I want to the program to randomly ADD .5 or SUBTRACT .5 to each trial.

I thought it would look something like this (but this does not work):

double randomOne = myRandom.nextDouble(-.5 || .5)
• ###### 4. Re: Random Number Selection Help
Currently Being Moderated
s_cheplick12 wrote:
I will try out both of your suggestions and write back in a little bit if they work. I want to the program to randomly ADD .5 or SUBTRACT .5 to each trial.
So you want my second solution or Enceph's solution.

>
I thought it would look something like this (but this does not work):

double randomOne = myRandom.nextDouble(-.5 || .5)
An interesting syntax!
• ###### 5. Re: Random Number Selection Help
Currently Being Moderated
Neither suggestion worked the way I wanted them to. I will try to work out the myRandom.nextBoolean suggestion above, but I'm not sure thats what I am looking for. I'd rather use a myRandom.nextInt or myRandom.nextDouble and have the program randomly add or subtract -.5
• ###### 6. Re: Random Number Selection Help
Currently Being Moderated
Use suggestion in reply 1. Pseudocode:
get random boolean;
if random boolean {
} else {
subtract 0.5;
}
• ###### 7. Re: Random Number Selection Help
Currently Being Moderated
sabre150 wrote:
s_cheplick12 wrote:
I will try out both of your suggestions and write back in a little bit if they work. I want to the program to randomly ADD .5 or SUBTRACT .5 to each trial.
So you want my second solution or Enceph's solution.
I thought it would look something like this (but this does not work):

double randomOne = myRandom.nextDouble(-.5 || .5)
An interesting syntax!
Sabre,

I tried out using your two suggestions, and they did not work the way I wanted. I'll try the boolean suggestion mentioned above, and write back how that worked out.

double randomOne = myRandom.nextDouble(-.5 || .5)
I actually think that if Java did have the power to read code that way it would be great.
• ###### 8. Re: Random Number Selection Help
Currently Being Moderated
s_cheplick12 wrote:
Neither suggestion worked the way I wanted them to.
My second solution and Enceph's solution will both do what you have just asked for.
I will try to work out the myRandom.nextBoolean suggestion above, but I'm not sure thats what I am looking for.
The we sure as hell have a problem with working out what you are looking for.
I'd rather use a myRandom.nextInt or myRandom.nextDouble and have the program randomly add or subtract -.5
Maybe you would but you have to use what is available.

Random.nextInt(2) gives either 0 or 1 uniformly distributed (50% chance of each happening on any call). Subtracting 0.5 from this values gives either -0.5 or +0.5 .
• ###### 9. Re: Random Number Selection Help
Currently Being Moderated
Sabre,

"Then we sure as hell have a problem with working out what you are looking for."

I'll try again. I must be moving to fast. I'll write back in a little bit and let you know how it worked. Stay tuned...
• ###### 10. Re: Random Number Selection Help
Currently Being Moderated
Now I'm confused as to the requirements. Are your trying to randomly add/subtract 0.5 to a random number or to some other variable?
• ###### 11. Re: Random Number Selection Help
Currently Being Moderated
I'm trying to randomly add or subtract .5 from the number 10 and see if it reaches 20 or 0 first. The purpose of my trial stems from a math project that I am working on. The program will end when it gets to 20 or 0 from a random process of adding or subtracting .5

note: I am a super newb at Java

Edited by: s_cheplick12 on Dec 14, 2009 8:46 PM
• ###### 12. Re: Random Number Selection Help
Currently Being Moderated
s_cheplick12 wrote:
I'm trying to randomly add or subtract .5 from the number 10 and see if it reaches 20 or 0 first. The purpose of my trial stems from a math project that I am working on. The program will end when it gets to 20 or 0 from a random process of adding or subtracting .5

note: I am a super newb at Java

Edited by: s_cheplick12 on Dec 14, 2009 8:46 PM
Then both Enceph's solution and my second solution will provide the random +0.5 or -0.5 that you need to ADD to the current summation at each iteration.
• ###### 13. Re: Random Number Selection Help
Currently Being Moderated
In that case I would go with sabre's second suggestion.
value += myRandom.nextInt(2) - 0.5;
• ###### 14. Re: Random Number Selection Help
Currently Being Moderated
Otherwise, you can save yourself a lot of trouble.
if(myRandom.nextBoolean()) {
System.out.println(20);
} else {
System.out.println(0);
}
Since you are guaranteed one of only two possibilites ;)
1 2 Previous Next