Results 1 to 10 of 10
  1. #1
    Spyderpig is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default 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. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Check out the Random class.

  3. #3
    Spyderpig is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default

    yea i know the random class. thanks you're very helpful....

  4. #4
    Spyderpig is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default

    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. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

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

  6. #6
    Spyderpig is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default

    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. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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. #8
    Spyderpig is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default

    thanks that helped a bit. +rep

  9. #9
    Spyderpig is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default

    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. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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.

Similar Threads

  1. Difficulties randomizing images in an array
    By phb5004 in forum New To Java
    Replies: 2
    Last Post: 12-11-2009, 05:45 AM
  2. randomizing an array HELP! please!
    By hugolord in forum New To Java
    Replies: 12
    Last Post: 08-10-2008, 04:36 AM

Posting Permissions

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