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:
Spring Boot - Thymeleaf
Validations for Enum Types
Java Program to Search for an Element in a Binary Search Tree
Convert Time to Milliseconds in Java
Java Program to Implement Efficient O(log n) Fibonacci generator
Biểu thức Lambda trong Java 8 – Lambda Expressions
Inject Parameters into JUnit Jupiter Unit Tests
Java Program to Represent Graph Using Adjacency Matrix
Spring Boot - Quick Start
Introduction to the Java NIO Selector
Spring Boot - Scheduling
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
How to Use if/else Logic in Java 8 Streams
Java Program to Implement Knight’s Tour Problem
Java Program to Perform Polygon Containment Test
Java Program to Find Nearest Neighbor for Static Data Set
Tạo ứng dụng Java RESTful Client không sử dụng 3rd party libraries
Java Program to Implement Levenshtein Distance Computing Algorithm
Spring REST with a Zuul Proxy
Java Program to Perform Stooge Sort
So sánh ArrayList và Vector trong Java
Java Program to Implement Sorted List
A Guide to Spring Boot Admin
Cơ chế Upcasting và Downcasting trong java
Quick Intro to Spring Cloud Configuration
How to Return 404 with Spring WebFlux
Java Program to Implement Splay Tree
Posting with HttpClient
Java Program to Find the Connected Components of an UnDirected Graph
Collect a Java Stream to an Immutable Collection
Jackson Exceptions – Problems and Solutions
Java Perform to a 2D FFT Inplace Given a Complex 2D Array