Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: Help me with using random numbers!

  1. #1
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Help me with using random numbers!

    Ok so heres a snipper of what I'm trying to do with a few colours

    Java Code:
    import java.util.Random;
    
    int random = randomNumbers.nextInt(256);		
    	Color color1 = new Color(random, random, random);
    I want to create a few colours this way (colours 1-10) but get the same product each time, I suppose the reason for this is that the values of random arent changing (however they do change if you resize the picture! but again they all change to the same product)

    my question is, do I really need to create 3*10 seperate instances of the Random class to get what I want, or is there a way for it to recalculate random for each argument?

    thank you experts

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,343
    Rep Power
    25

    Default Re: Help me with using random numbers!

    but get the same product each time
    Could you print out and post the values you are getting to show what you mean by "the same product each time"?
    You don't show enough code to know what it is doing.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Help me with using random numbers!

    Typically a program will create just one instance of Random. But call its methods as many times as needed.

    Java Code:
    import java.awt.Color;
    import java.util.Random;
    
    public class Foo {
        public static void main(String[] args) {
                // create it once...
            Random rand = new Random();
            
            for(int times = 0; times < 42; times++) {
                    // ... but use it many times
                Color color = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
                System.out.println("Random colour is " + color);
            }
        }
    }
    EscSequenceAlpha likes this.

Similar Threads

  1. Is Random() Only For Numbers?
    By Salamander in forum New To Java
    Replies: 2
    Last Post: 02-07-2011, 10:02 AM
  2. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  3. Help with random numbers
    By checkmylongboarding in forum New To Java
    Replies: 2
    Last Post: 01-12-2009, 05:47 AM
  4. Random numbers
    By jithan in forum Advanced Java
    Replies: 3
    Last Post: 06-14-2008, 02:04 PM
  5. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM

Posting Permissions

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