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:
Guide to Selenium with JUnit / TestNG
Shuffling Collections In Java
Disable Spring Data Auto Configuration
Java Program to Implement Gale Shapley Algorithm
@Order in Spring
Adding Shutdown Hooks for JVM Applications
Java Program to Implement Hash Tables chaining with Singly Linked Lists
Sử dụng CyclicBarrier trong Java
Java Program to Implement Nth Root Algorithm
Java Program to Implement RoleList API
Guide to java.util.concurrent.Locks
Java 8 StringJoiner
Java Program to Implement Park-Miller Random Number Generation Algorithm
Java Program to Implement Quick Sort Using Randomization
Java Program to Implement Double Ended Queue
Setting Up Swagger 2 with a Spring REST API
Exception Handling in Java
Java Program to Implement Word Wrap Problem
Java Program to Implement LinkedBlockingDeque API
Java List UnsupportedOperationException
A Guide to Concurrent Queues in Java
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
ETags for REST with Spring
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
Java Program to Implement Max-Flow Min-Cut Theorem
Java Program to Find the GCD and LCM of two Numbers
Java – Get Random Item/Element From a List
The DAO with Spring and Hibernate
Derived Query Methods in Spring Data JPA Repositories
Tạo chương trình Java đầu tiên sử dụng Eclipse
The Java 8 Stream API Tutorial
Runnable vs. Callable in Java