Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: How to use a Random object here?

  1. #1
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default How to use a Random object here?

    Hey guys, I have a problem here which I am trying to fix. I have a for loop, and inside it there are 3 cases that I want to implement randomly. The cases involve asteroids falling down a screen on different directions. What I want to do is to use a random variable so that somehow each case is selected randomly (for example, the random var turns into 1 and then the case associated with 1 occurs, and then start over again.

    I tried using a Random Array List called randoms, but it doesn't seem to work. Could anyone tell me what is wrong here?

    // draw asteroid
    		for(int i = 0; i <= asteroids.size() - 1; i++)
    		{
     
    			if(!status.isNewAsteroid())
     
     
    				{
     
    						int loop = randoms.get(i).nextInt(2);
    						if(loop == 0)
    						{
     
    							asteroids.get(i).translate(0, asteroids.get(i).getSpeed());
    							// Displays one asteroid on the screen:
    							graphicsMan.drawAsteroid(asteroids.get(i), g2d, this);
     
    				}
     
    						else if(loop == 1)
    						{
    							asteroids.get(i).translate(asteroids.get(i).getSpeed(), asteroids.get(i).getSpeed());
    							// Displays one asteroid on the screen:
    							graphicsMan.drawAsteroid(asteroids.get(i), g2d, this);
    						}
     
    						else if(loop == 2)
    						{
    							asteroids.get(i).translate(-asteroids.get(i).getSpeed(), asteroids.get(i).getSpeed());
    							// Displays one asteroid on the screen:
    							graphicsMan.drawAsteroid(asteroids.get(i), g2d, this);
    						}
     
    				}


    Is there an alternate way of doing this? Or maybe could someone give me a tip? Thanks you all very much.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to use a Random object here?

    it doesn't seem to work.
    Can you explain what the current code does and give some examples and explain what you want it to do differently.
    Is there an alternate way of doing this?
    No way to understand what the code is doing without definitions and current contents for all the variables.

    Try writing a small simple program that uses Random that shows what you are trying to do.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to use a Random object here?

    Quote Originally Posted by Norm View Post
    Can you explain what the current code does and give some examples and explain what you want it to do differently.

    No way to understand what the code is doing without definitions and current contents for all the variables.

    Try writing a small simple program that uses Random that shows what you are trying to do.
    Ok, I will try to do a more specific description here.

    When I run this code:

    asteroids.get(i).translate(0, asteroids.get(i).getSpeed());
    						// Displays one asteroid on the screen:
    						graphicsMan.drawAsteroid(asteroids.get(i), g2d, this);

    The asteroids fall down vertically like this:

    asteroid1.jpg photo by jean28x | Photobucket


    When I run this other code:

    asteroids.get(i).translate(asteroids.get(i).getSpeed(), asteroids.get(i).getSpeed());
    						// Displays one asteroid on the screen:
    						graphicsMan.drawAsteroid(asteroids.get(i), g2d, this);

    The asteroids fall down diagonally like this:

    http://i1226.photobucket.com/albums/.../asteroid2.jpg

    The third case makes the asteroids fall down diagonally as well but from left to right instead of from right to left.

    What I want to do is to make the asteroids fall down in a mixture of these three cases, but randomly selected. Ideally, I would like to see something like this:

    http://i1226.photobucket.com/albums/.../asteroid3.jpg


    My idea is to implement a random variable from 0 to 2, and depending on the number that gets selected randomly, the case associated with that number occurs.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to use a Random object here?

    My idea is to implement a random variable from 0 to 2,
    See the Random class for methods to do that. To work out the technique write a short program that calls the Random class's method repeatedly (in a loop) and prints out the results.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to use a Random object here?

    Quote Originally Posted by Norm View Post
    See the Random class for methods to do that. To work out the technique write a short program that calls the Random class's method repeatedly (in a loop) and prints out the results.
    I saw that in order to get 3 random variables I need to set the nextInt to 2 so that I get a 3 possible numbers: 0,1, and 2. That is why I put 2 inside the nextInt that gets the random objects from the Array List:

     int loop = randoms.get(i).nextInt(2);

    What I don't know very well is how to use the variable loop so that depending on it's value, a certain case occurs, and then it selects another random value and selects it's respective case, etc.

    --- Update ---

    Maybe I have a problem with the Random Array List? This is how I set it up:

    public ArrayList<Random> getRandomArray()
    	{
    		Random jean1 = new Random();
    		Random jean2 = new Random();
    		Random jean3 = new Random();
     
    		randoms.add(jean1);
    		randoms.add(jean2);
    		randoms.add(jean3);
     
    		return randoms;
    	}

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to use a Random object here?

    I don't know what the code you are posting does because its not all there.


    Is this what you are trying to do: Assign a random value in the range 0 to 2 to the variable: loop?
    The Random class has a method that will return that kind of value.

    To see how to use the Random class's methods, you should write a small test program.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: How to use a Random object here?

    Quote Originally Posted by Norm View Post
    I don't know what the code you are posting does because its not all there.


    Is this what you are trying to do: Assign a random value in the range 0 to 2 to the variable: loop?
    The Random class has a method that will return that kind of value.

    To see how to use the Random class's methods, you should write a small test program.
    I found this example online that looks dangerously similar to what I am trying to do, but to a much simpler extent:

    private static String person() {
            String name;
            switch (rand(3)) {
                case 0 : name = "idiot"   ; break;
                case 1 : name = "imbecile"; break;
                case 2 : name = "moron"   ; break;
                default: name = "bad value from person???";
            }
            return name;
        }

    How would I do to make that rand variable change after every loop?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How to use a Random object here?

    make that rand variable change after every loop?
    rand() is a method not a variable. What is the definition for the rand() method?

    Are you asking how to change what the rand() method returns every time it is called?
    You'll have to post the code for rand().
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Convert File Object to class<?> object
    By CEO in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 27th, 2012, 06:55 PM
  2. Creating object everytime object is called
    By aandcmedia in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 12th, 2012, 04:18 PM
  3. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  4. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM
  5. Generation of random number using random class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 16th, 2009, 06:10 AM