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 8 Collectors toMap
Anonymous Classes in Java
Spring Security Registration – Resend Verification Email
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Java Program to Describe the Representation of Graph using Adjacency List
Zipping Collections in Java
Iterating over Enum Values in Java
Spring Data JPA Delete and Relationships
Guide to WeakHashMap in Java
Posting with HttpClient
Java Program to Describe the Representation of Graph using Adjacency Matrix
Comparing Arrays in Java
Spring Boot - Google Cloud Platform
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Fixing 401s with CORS Preflights and Spring Security
Function trong Java 8
Map to String Conversion in Java
Spring Boot - Servlet Filter
RestTemplate Post Request with JSON
Java Program to Implement Multi-Threaded Version of Binary Search Tree
Introduction to Spring MVC HandlerInterceptor
Java Program to Implement Floyd-Warshall Algorithm
Converting a List to String in Java
Quick Guide to Spring Controllers
Spring Webflux with Kotlin
Hướng dẫn Java Design Pattern – Factory Method
Kết hợp Java Reflection và Java Annotations
Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
Hướng dẫn sử dụng Java Reflection
Java Program to Implement Heap’s Algorithm for Permutation of N Numbers
Immutable Objects in Java
REST Web service: Upload và Download file với Jersey 2.x