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:
How to Store Duplicate Keys in a Map in Java?
Spring Boot - Database Handling
Java Program to Implement Maximum Length Chain of Pairs
Java – File to Reader
Java Program to Implement LinkedList API
Java Program to Implement LinkedBlockingQueue API
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Integer Constant Pool trong Java
Tránh lỗi NullPointerException trong Java như thế nào?
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Converting a List to String in Java
Java Program to Check if it is a Sparse Matrix
Guide to Spring @Autowired
Java Program to Perform Right Rotation on a Binary Search Tree
HandlerAdapters in Spring MVC
The Java 8 Stream API Tutorial
“Stream has already been operated upon or closed” Exception in Java
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Removing all Nulls from a List in Java
Java Program to Implement Sieve Of Eratosthenes
Thao tác với tập tin và thư mục trong Java
Convert Time to Milliseconds in Java
@DynamicUpdate with Spring Data JPA
OAuth 2.0 Resource Server With Spring Security 5
The Order of Tests in JUnit
How to Define a Spring Boot Filter?
Java Program to Implement Ternary Search Tree
Format ZonedDateTime to String
Java Program to Perform the Unique Factorization of a Given Number
The Registration API becomes RESTful
Spring Cloud Bus
Convert Hex to ASCII in Java