Help with Randomizing

• 02-18-2011, 05:35 AM
Spyderpig
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?
• 02-18-2011, 05:42 AM
Junky
Check out the Random class.
• 02-18-2011, 05:57 AM
Spyderpig
yea i know the random class. thanks you're very helpful....
• 02-18-2011, 06:03 AM
Spyderpig
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
• 02-18-2011, 06:07 AM
Junky
Quote:

i cant seem to get the random right
• 02-18-2011, 06:09 AM
Spyderpig
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);
• 02-18-2011, 06:13 AM
Junky
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),
• 02-18-2011, 06:18 AM
Spyderpig
thanks that helped a bit. +rep
• 02-18-2011, 06:22 AM
Spyderpig
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)

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);                         }                 }                 } }```
• 02-18-2011, 06:34 AM
Junky
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.