The Artima Developer Community
Sponsored Link

Java Buzz Forum
How to Generate Random Number between 1 to 10 - Java Example

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Javin Paul

Posts: 1090
Nickname: javinpaul
Registered: Jan, 2012

Javin Paul is Java Programmer working on Finance domain.
How to Generate Random Number between 1 to 10 - Java Example Posted: Jan 24, 2015 1:57 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Javin Paul.
Original Post: How to Generate Random Number between 1 to 10 - Java Example
Feed Title: Java67
Feed URL: http://www.java67.com/feeds/posts/default?alt=rss
Feed Description: Java and technology tutorials, tips, questions for all programmers.
Latest Java Buzz Posts
Latest Java Buzz Posts by Javin Paul
Latest Posts From Java67

Advertisement
There are many ways to generate random numbers in Java e.g. Math.random() utility function, java.util.Random class or newly introduced ThreadLocalRandom and SecureRandom, added on JDK 1.7. Each has there own pros and cons but if your requirement is simple, you can generate random numbers in Java by using Math.random() method. This method returns a psuedorandom positive double value between 0.0 and 1.0, where 0.0 is inclusive and 1.0 is exclusive. It means Math.random() always return a number greater than or equal to 0.0 and less than 1.0. Internally it uses java.util.Random class. So when you first call this method, it creates instance of Random class and cache it for future use. Any further call is just equivalent of Random.nextDouble(). If your requirement is more sophisticated i.e. you need random numbers between a range or multiple threads needs to generate random number simultaneously, then you should look other random solution available in Java. Since Math.random() method is properly synchronized to ensure correct value is returned when used by multiple thread, it also become bottleneck when multiple thread simultaneously uses it. To solve this problem, JDK 1.7 introduces ThreadLocalRandom class, which allows each thread to keep their own psuedo random number to reduce contention. In a scalable environment, ThreadLocalRandom can improve performance significantly as it keeps the instance of random number generator in a ThreadLocal variable to reduce contention. If security is your concern then you have another option in terms of SecureRandom, which provides a cryptographically strong random number generator.
Read more ยป

Read: How to Generate Random Number between 1 to 10 - Java Example

Topic: The GWT Java to JavaScript Compiler Previous Topic   Next Topic Topic: Using Java 8 to Prevent Excessively Wide Logs

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use