# Thread: Help with Randomizing

1. Member
Join Date
Feb 2011
Posts
24
Rep Power
0

## Help with Randomizing

I need to determine if the price will increase or decrease (50% chance for either one)and also determine by how much the stock will change by generating a random integer from 1 to 10 (\$1-\$10) for 3 different variables. Any ideas on the easiest way to do this?

2. Check out the Random class.

3. Member
Join Date
Feb 2011
Posts
24
Rep Power
0
yea i know the random class. thanks you're very helpful....

4. Member
Join Date
Feb 2011
Posts
24
Rep Power
0
Java Code:
```import java.util.*;

public class Test {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);

System.out.println("Please enter a seed value [integer]:");

int pORNG = 1;

int seed = in.nextInt();
Random rng = new Random(seed);

int change = rng.nextInt(2);
int sChange = rng.nextInt(10);
if (change == 1)
{
pORNG = pORNG + sChange;
System.out.println("Orange Inc. Increases");
}
else
{
pORNG = pORNG - sChange;
System.out.println("Orange Inc. Decreases");
}
System.out.print(pORNG);
}

}```
this is what i have so far, i cant seem to get the random right

5. i cant seem to get the random right
What does that mean? Care to provide more information.

6. Member
Join Date
Feb 2011
Posts
24
Rep Power
0
okay so i need to randomly decide if the price will increase or decrease so i wrote:

int change = rng.nextInt(2);

im not sure if this is the right code to generate a random integer thats either a 1 or a 2

im trying to do the same thing in the next line, generate a random integer between 1 and 10.

int sChange = rng.nextInt(10);

7. What you would do is extensively test your code to see if it does what it should. If you test it once it might behave as expected. It might also behave for 999 test but then on the 1000th test. Boom! I draw your attention to the API for the nextInt method:

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive),

8. Member
Join Date
Feb 2011
Posts
24
Rep Power
0
thanks that helped a bit. +rep

9. Member
Join Date
Feb 2011
Posts
24
Rep Power
0
one last question. i got it to randomize for one variable, now how do i make it loop for the other 2? (int pMCRO = 1 & int pGUUG = 1)

Java Code:
```	public static void main(String[] args) {
Scanner in = new Scanner(System.in);

System.out.println("Please enter a seed value [integer]:");

int pORNG = 1;
int pMCRO = 1;
int pGUUG = 1;

int seed = in.nextInt();
Random rng = new Random(seed);

for (int i =1; i <=10; i++)
{
int change = rng.nextInt(2);
int sChange = rng.nextInt(11);
if (change == 0)
{
pORNG = pORNG + sChange;
System.out.println("Orange Inc. Increases by " + sChange);
System.out.println("New price is: " + pORNG);
}
else
{
pORNG = pORNG - sChange;
System.out.println("Orange Inc. Decreases " + sChange);
System.out.println("New price is: " + pORNG);
}
}

}

}```

10. Java Code:
`int sChange = rng.nextInt(11);`
This is incorrect. It generates random numbers from 0 to 10 but your instructions say it should be 1 to 10.

What you can do is move the code that generates the random increase/decrease into a method. Have that method return a value and then add it to your variables. Which means calling that method 3 times.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•