• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Weird Random Number Failure

 
Greenhorn
Posts: 21
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I'm trying to generate a random long, preferably in the full range between Long.MIN_VALUE and Long.MAX_VALUE. My first (very naive) attempt was "Long.MIN_VALUE + (long) (Math.Random() * (Long.MAX_VALUE - Long.MIN_VALUE + 1));", obviously this didn't work because the calculation overflowed. I eventually got lazy and decided to just use the positive numbers, so I tried "(long) Math.Random()*(Long.MAX_VALUE);" but for some reason this always returns 0. I'd really like to know why this doesn't work and I'd appreciate any ideas on how to accomplish my goal. I thought about using a BigInteger, but this code needs to be speedy. Thanks for your help.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(long) Math.Random()*(Long.MAX_VALUE) will always return 0, because you first cast the Math.Random() to long, which strips off decimal digits from it and therefore always results in zero.

Have a look at the java.util.Random class. It can probably give you all you need.
 
Marshal
Posts: 79698
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree the java.util.Random class’ method is a better solution, but you can sort out that multiplication and cast if you remember that cast has a higher precedence than * and add one pair of () appropriately.
 
Daniel Croft
Greenhorn
Posts: 21
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, I can't believe I didn't see that. I guess that's what you get for coding in the middle of the night. Thanks guys
 
Campbell Ritchie
Marshal
Posts: 79698
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome
 
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic