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 – String to Reader
Java Program to Compute the Volume of a Tetrahedron Using Determinants
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
Java Program to Implement Lloyd’s Algorithm
Java Switch Statement
Daemon Threads in Java
Java Program to Perform Cryptography Using Transposition Technique
Java Program to Implement Gauss Jordan Elimination
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
Guide to Java 8 groupingBy Collector
Java Multi-line String
Java Program to Create a Balanced Binary Tree of the Incoming Data
Removing all duplicates from a List in Java
Java Program to Check if a Matrix is Invertible
Java Program to Construct K-D Tree for 2 Dimensional Data
Java Program to Implement HashTable API
Base64 encoding và decoding trong Java 8
Hướng dẫn sử dụng Java Reflection
Chuyển đổi giữa các kiểu dữ liệu trong Java
Default Password Encoder in Spring Security 5
Java Program to Implement Bubble Sort
Creating a Custom Starter with Spring Boot
What is Thread-Safety and How to Achieve it?
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
New Features in Java 9
Ép kiểu trong Java (Type casting)
Java Program to Implement Sparse Array
The DAO with JPA and Spring
Spring Boot - CORS Support
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
Tránh lỗi ConcurrentModificationException trong Java như thế nào?
Convert a Map to an Array, List or Set in Java