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:
Java Program to Perform Search in a BST
Java Program to Implement Find all Forward Edges in a Graph
Java Program to Implement Pairing Heap
Custom HTTP Header with the HttpClient
Extra Login Fields with Spring Security
Java Program to Implement Min Heap
Merging Two Maps with Java 8
Java Program to Implement the linear congruential generator for Pseudo Random Number Generation
Java Program to Implement Knapsack Algorithm
Spring Data – CrudRepository save() Method
New Features in Java 8
Java Program to Implement Shoelace Algorithm
Guava – Join and Split Collections
Chuyển đổi từ HashMap sang ArrayList
Câu lệnh điều khiển vòng lặp trong Java (break, continue)
Get and Post Lists of Objects with RestTemplate
Java Program to Implement Bellman-Ford Algorithm
Encode a String to UTF-8 in Java
Quick Guide to Spring Bean Scopes
Java – Try with Resources
Java Program to Implement Euclid GCD Algorithm
Predicate trong Java 8
The HttpMediaTypeNotAcceptableException in Spring MVC
Zipping Collections in Java
Java Program to Implement Insertion Sort
Bootstrap a Web Application with Spring 5
ClassNotFoundException vs NoClassDefFoundError
Spring Cloud AWS – Messaging Support
Remove HTML tags from a file to extract only the TEXT
Java Program to Perform Uniform Binary Search
Guide to WeakHashMap in Java
Introduction to Netflix Archaius with Spring Cloud