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:
New Stream Collectors in Java 9
Spring Cloud Connectors and Heroku
Bootstrap a Web Application with Spring 5
Spring RestTemplate Request/Response Logging
Object Type Casting in Java
Flattening Nested Collections in Java
Inheritance with Jackson
Chuyển đổi giữa các kiểu dữ liệu trong Java
Java Program to Represent Linear Equations in Matrix Form
Tìm hiểu về xác thực và phân quyền trong ứng dụng
Inject Parameters into JUnit Jupiter Unit Tests
The Difference Between map() and flatMap()
Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java
Java Program to Find kth Smallest Element by the Method of Partitioning the Array
Exploring the Spring Boot TestRestTemplate
Guava – Join and Split Collections
Java Program to Find kth Largest Element in a Sequence
Reversing a Linked List in Java
Java Multi-line String
The Difference Between Collection.stream().forEach() and Collection.forEach()
Java Program to Implement Hash Tables
Getting a File’s Mime Type in Java
Java Program to Implement the linear congruential generator for Pseudo Random Number Generation
Java Program to Find Minimum Element in an Array using Linear Search
Spring Boot - Zuul Proxy Server and Routing
Compact Strings in Java 9
Java Program to Encode a Message Using Playfair Cipher
Java Program to Implement Gale Shapley Algorithm
Java Program to Implement ConcurrentLinkedQueue API
Constructor Dependency Injection in Spring
Guide to Guava Table
Java Program to Implement Euclid GCD Algorithm