• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Truly Random Number

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is possible to generate a truly random number using Java or do I need to use a third party SDK or API? I need to generate 1000's of random numbers at the same time and I'm seeing lots of duplicates using the traditional method.
 
Saloon Keeper
Posts: 9734
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are websites that can produce truly random numbers using a Cesium (or other) source. Some may be free for small quantities of random numbers and others may charge if you'll be needing large quantities of them, but they are truly random.
 
lowercase baba
Posts: 13082
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is your definition of "truly random"?  Using java, you can get random-seeming numbers - i.e numbers that are equally distributed across a range and that are (almost) impossible to predict.  But any pure-code implemented algorithm is pretty much guaranteed to not be completely random, since anyone who a) knows the algorithm, and b) knows the seed will be able to reproduce the sequence.
 
Carey Brown
Saloon Keeper
Posts: 9734
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"I'm seeing lots of duplicates": by its very definition, random numbers do not guarantee uniqueness.

A problem I had with pseudo-random numbers is that they are mostly "evenly distributed". This was readily apparent in a game I was working on 10 years ago. I was unable to come up with a trick to randomize the random numbers which is how I tripped across the website (sorry, don't remember the name or URL).
 
Sheriff
Posts: 17321
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Ashley McScott wrote:I need to generate 1000's of random numbers at the same time and I'm seeing lots of duplicates using the traditional method.


As Carey pointed out, randomness and uniqueness are two different things. Random sequences do not guarantee uniqueness. If you need both randomness and uniqueness, then first generate a list of unique numbers and then shuffle them to arrange them in some random order, then iterate over the list.
 
Bartender
Posts: 2908
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are looking for a unique id to be generated every time, have a look at universally unique identifier (UUID) :
The output will be always unique
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also use ULID instead of UUID.
 
Ashley McScott
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all, this has been very helpful.
 
Watchya got in that poodle gun? Anything for me? Or this tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic