| Author |
Generating random values for colours
|
tan kian
Ranch Hand
Joined: Apr 24, 2006
Posts: 30
|
|
i'm using this code to generate the color values, but it sucks =( everytime i open the emulator, it gives me the same color combination. i guess there must be something wrong with my code. can someone help? thanks.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9946
|
|
you're creating your random number generator with the same seed every time. this is great for debugging/testing, but not so good for production. try this: Random randcolor = new Random();
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
tan kian
Ranch Hand
Joined: Apr 24, 2006
Posts: 30
|
|
|
havent tried it yet, but if i use that, wont it return a value more than 255?
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9946
|
|
from the API, which if you don't know how to use, you should learn: "If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers." the value you use when creating the Random object does NOT set the size of the returned value, it just sets the seed value. i think what you need to do is create your random class with no value, and then when you call nextInt(), pass 256 in there. i'm not sure why you are doing the bit shifting - can you explain that?
|
 |
tan kian
Ranch Hand
Joined: Apr 24, 2006
Posts: 30
|
|
i just copied the bit shifting off somewhere else as for nextInt, the method does not allow me to put any parameters in it, which was why i tried putting it when i created randcolor. i checked the API for that already. anyway i took out the numbers when i created randcolor and it works fine so far. thanks!!! [ May 22, 2006: Message edited by: tan kian ]
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9946
|
|
|
double check the api. nextInt DOES allow you to pass it an int.
|
 |
tan kian
Ranch Hand
Joined: Apr 24, 2006
Posts: 30
|
|
|
the link is for j2se. i'm using j2me(j2me wireless toolkit ktoolbar) sadly.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9946
|
|
|
ah, crap... sorry. it's too early for me to be posting.
|
 |
 |
|
|
subject: Generating random values for colours
|
|
|