• 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 Class

 
Ranch Hand
Posts: 50
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I was looking at a post earlier by Takanori and Campbell advised to

Avoid messing around with Math.random().



I read the API's about Random in both the API docs for class Random and class Math, but as a beginner my understanding is not great.
Could someone elaborate on why the one in the Math class is to be avoided?
I understand that they both came out in JDK 1.0, so from that I would interpret that they both have their uses?

thanks,
Daniel
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's mainly to keep it simpler. Math.random() just returns a random number between 0 and 1. If you want something else - which is probably the most common situation - you have to do the translation yourself. Random has a higher level API that lets you get some common requirements more directly.

There are also certain advantages in multi-threaded applications (you can let each thread, or each component, have its own generator to reduce synchronization conflicts), and sometimes the ability to generate a "repeatable" random sequence (by setting the seed to a specific value) is useful. But for general use I think the API is the most important factor.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want a number between 0.0 and 0.9999999999999… inclusive, then Math#random does what you want. If, however, you want what was required in the other thread, viz. a non‑negative whole number randomly chosen less than 7, then Random has a method which will do exactly that. No need for multiplication, casts or anything like that. Remember, (int) Math.random() * 7 will do exactly what you don't want, so the casting is error‑prone, too.


If you want something chosen randomly with 50% probability, try this:-
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Hirning wrote: . . . I understand that they both came out in JDK 1.0, . . .

Actually Math.random() uses a Random object to create its output.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote: . . . you can let each thread, or each component, have its own generator to reduce synchronization conflicts . . .

I thought java.util.Random was thread‑safe. But it does say in the link that you may get poor performance and suggests something different for multithreaded use.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Matthew Brown wrote: . . . you can let each thread, or each component, have its own generator to reduce synchronization conflicts . . .

I thought java.util.Random was thread‑safe. But it does say in the link that you may get poor performance and suggests something different for multithreaded use.



I think that's the point - it's thread safe, but that means that if you've got lots of threads hitting it then they'll be blocked. Math.random only uses a single generator, but using Random you can decide how many to use, and provide multiple ones if you need to for performance.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that sounds like what the API said.
 
Daniel Hirning
Ranch Hand
Posts: 50
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That clears it up I think. Class Math while simpler to use is not thread safe, and as I understand it, casting is to be avoided where possible due to loss of bits.
While Class Random has more powerful and specific tools and is somewhat thread safe.

Thanks Matthew/Campbell.

cheers,
Daniel

 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nearly. I do not know whether Math#random is thread‑safe. The problem with multithreading is that you get slow performance.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Hirning wrote:That clears it up I think. Class Math while simpler to use is not thread safe


Actually, I'm pretty sure Math.random() uses a Random object behind the scenes, so it will be as Thread-safe (or not) as Random itself. But Math simpler? Your definition is obviously different from mine...

Winston
 
Daniel Hirning
Ranch Hand
Posts: 50
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Your definition is obviously different from mine...



I see your point the Random class methods that i have tried are specific and easy.

What i meant was simpler for a beginner, in that no imports are required and that as its a method there is not a whole lot to learn unlike introducing a new class! Also easy to remember Math.Random..couldn't forget that one if you tried!
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is a lot more difficult to writethanIt is much more difficult to see which of those code snippets contains a nasty error.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic