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 Implement AVL Tree
Java Program to Solve a Matching Problem for a Given Specific Case
LinkedList trong java
Consuming RESTful Web Services
Hướng dẫn Java Design Pattern – State
Spring Boot: Customize the Jackson ObjectMapper
Java Program to Perform Addition Operation Using Bitwise Operators
Java – Reader to InputStream
Tạo ứng dụng Java RESTful Client không sử dụng 3rd party libraries
Java Program to Print only Odd Numbered Levels of a Tree
Lớp TreeMap trong Java
Java Program to Solve any Linear Equation in One Variable
Cachable Static Assets with Spring MVC
Introduction to the Functional Web Framework in Spring 5
Java Program to Implement Johnson’s Algorithm
Java Program to Implement Tarjan Algorithm
Rate Limiting in Spring Cloud Netflix Zuul
Jackson vs Gson
Java Program to Implement Unrolled Linked List
A Guide to HashSet in Java
Summing Numbers with Java Streams
OAuth2 for a Spring REST API – Handle the Refresh Token in Angular
RestTemplate Post Request with JSON
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
Java Program to Implement Graph Coloring Algorithm
Java Program to Implement Range Tree
Remove the First Element from a List
ThreadPoolTaskExecutor corePoolSize vs. maxPoolSize
Hướng dẫn Java Design Pattern – Decorator
Spring Security – Reset Your Password
Java Program to Represent Graph Using Incidence List