• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Generating random values for colours

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
lowercase baba
Posts: 13056
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13056
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13056
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
double check the api. nextInt DOES allow you to pass it an int.
 
tan kian
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the link is for j2se. i'm using j2me(j2me wireless toolkit ktoolbar) sadly.
 
fred rosenberger
lowercase baba
Posts: 13056
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ah, crap... sorry. it's too early for me to be posting.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic