Results 1 to 3 of 3
- 05-06-2012, 02:19 AM #1
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Help me with using random numbers!
Ok so heres a snipper of what I'm trying to do with a few colours
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)Java Code:import java.util.Random; int random = randomNumbers.nextInt(256); Color color1 = new Color(random, random, random);
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
- 05-06-2012, 02:40 AM #2
Re: Help me with using random numbers!
Could you print out and post the values you are getting to show what you mean by "the same product each time"?but get 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.
- 05-06-2012, 02:42 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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); } } }
Similar Threads
-
Is Random() Only For Numbers?
By Salamander in forum New To JavaReplies: 2Last Post: 02-07-2011, 10:02 AM -
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
Help with random numbers
By checkmylongboarding in forum New To JavaReplies: 2Last Post: 01-12-2009, 05:47 AM -
Random numbers
By jithan in forum Advanced JavaReplies: 3Last Post: 06-14-2008, 02:04 PM -
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01-17-2008, 10:44 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks