Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Truly Random Number

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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: 6419
60
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • 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: 12773
51
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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: 6419
60
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • 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).
 
Marshal
Posts: 14386
240
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • 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: 2406
106
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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: 21817
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use ULID instead of UUID.
 
Ashley McScott
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all, this has been very helpful.
 
World domination requires a hollowed out volcano with good submarine access. Tiny ads are optional.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!