• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

random background color choosing in jsp

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have a code in which I use Math.random() to select from a set of 3 colors. When I go on refreshing the browser (Firefox) it keeps displaying the background in one of the three colors. But a couple of times I also had "white" as the background color.

MY QUESTION: Why do I get white background even though it is not one of the colors I have in my set?

Thanks

Ricky

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In that code you create five different random numbers. If the first is less than 0.3, you use green. If the second is greater than 0.3 and the third is less than 0.5, you use blue. If the fourth is greater than 0.5 and the fifth is less than 0.8, you use red.

There's plenty of scope for not using any of those three colours here; for example if the first was 0.72 and the second was 0.117 and the fourth was 0.44.

But obviously you meant to use only one random number. If you fixed your code to get a random number and assign it to a local variable, then to always compare against that variable, you would then only have to worry about what would happen if it was over 0.8.
 
Ricky James
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Clapham:
In that code you create five different random numbers. If the first is less than 0.3, you use green. If the second is greater than 0.3 and the third is less than 0.5, you use blue. If the fourth is greater than 0.5 and the fifth is less than 0.8, you use red.

There's plenty of scope for not using any of those three colours here; for example if the first was 0.72 and the second was 0.117 and the fourth was 0.44.

But obviously you meant to use only one random number. If you fixed your code to get a random number and assign it to a local variable, then to always compare against that variable, you would then only have to worry about what would happen if it was over 0.8.



O I got it.

Thank you so much Paul. Much appreciated.

Ricky
 
Ranch Hand
Posts: 243
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was cool Paul
 
Anderson gave himself the promotion. So I gave myself this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic