This is a java program to generate random numbers using rand() and srand() functions. Java provides Random class that generates a random numbers. rand() gives long random numbers. srand() provides unique numbers.
Here is the source code of the Java Program to Use rand and srand Functions. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
//This is a sample program to generate a random numbers using rand() and srand()
//rand() gives long random numbers
//srand() provides unique numbers
import java.util.Random;
import java.util.UUID;
public class Rand_and_Srand
{
public static void main(String args[])
{
System.out.println("The numbers using rand");
for(int i=0; i<5; i++)
{
Random rand = new Random();
System.out.println(Math.abs(rand.nextInt()));
}
System.out.println("The numbers using srand");
for(int i=0; i<5; i++)
{
System.out.println(Math.abs(UUID.randomUUID().getMostSignificantBits()));
}
}
}
Output:
$ javac Rand_and_Srand.java $ java Rand_and_Srand The numbers using rand 1339557437 636169175 1207287888 1539694038 1040189301 The numbers using srand 301709257092546335 8798470719933102847 3480203219570178904 3272351410737399038 2158529096808811162
Related posts:
Converting Between an Array and a Set in Java
Prevent Cross-Site Scripting (XSS) in a Spring Application
A Guide to the finalize Method in Java
Java Program to Perform Searching Based on Locality of Reference
Java Program to Implement Caesar Cypher
Reactive Flow with MongoDB, Kotlin, and Spring WebFlux
Spring Security 5 – OAuth2 Login
Guide to Spring @Autowired
Serverless Functions with Spring Cloud Function
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Java Program to Optimize Wire Length in Electrical Circuit
Java – Reader to InputStream
A Guide to TreeMap in Java
Count Occurrences of a Char in a String
A Guide to HashSet in Java
Java Program to Perform Left Rotation on a Binary Search Tree
A Guide to Java 9 Modularity
Java Program to Find Transitive Closure of a Graph
Handle EML file with JavaMail
Spring Boot - Batch Service
Java Program to Implement the Checksum Method for Small String Messages and Detect
Generating Random Dates in Java
LinkedList trong java
Comparing Two HashMaps in Java
Java Program to Convert a Decimal Number to Binary Number using Stacks
Java Program to Implement Knight’s Tour Problem
Tạo ứng dụng Java RESTful Client không sử dụng 3rd party libraries
Introduction to Spring Method Security
Spring RequestMapping
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Java Program to Implement Heap
Sử dụng Fork/Join Framework với ForkJoinPool trong Java