Java Program to Use rand and srand Functions

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:

Using Spring @ResponseStatus to Set HTTP Status Code
Java Program to Implement Max-Flow Min-Cut Theorem
Java Program to Implement HashMap API
Spring Boot - Servlet Filter
File Upload with Spring MVC
Jackson JSON Views
Bootstrapping Hibernate 5 with Spring
Giới thiệu về Stream API trong Java 8
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Spring Boot - Tomcat Deployment
Lập trình đa luồng với CompletableFuture trong Java 8
Java Program to Find Number of Articulation points in a Graph
New Features in Java 13
Java – Combine Multiple Collections
Cơ chế Upcasting và Downcasting trong java
Spring Security Login Page with React
The Registration Process With Spring Security
Quick Guide to java.lang.System
Spring Boot - Cloud Configuration Server
A Guide to LinkedHashMap in Java
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Java Program to Implement the Program Used in grep/egrep/fgrep
Java Program to Check if a Directed Graph is a Tree or Not Using DFS
Java Program to Implement Hash Tables chaining with Singly Linked Lists
Java Program to Generate Date Between Given Range
Java Program to Optimize Wire Length in Electrical Circuit
Java Program to Find MST (Minimum Spanning Tree) using Prim’s Algorithm
Java Program to Implement Unrolled Linked List
Guide to the Synchronized Keyword in Java
Checking for Empty or Blank Strings in Java
Java Web Services – JAX-WS – SOAP
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS