This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generating random values for colours

 
tan kian
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12020
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
tan kian
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
havent tried it yet, but if i use that, wont it return a value more than 255?
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12020
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12020
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
double check the api. nextInt DOES allow you to pass it an int.
 
tan kian
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the link is for j2se. i'm using j2me(j2me wireless toolkit ktoolbar) sadly.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12020
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah, crap... sorry. it's too early for me to be posting.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic