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:

Spring Boot - Scheduling
Set Interface trong Java
Biểu thức Lambda trong Java 8 – Lambda Expressions
Java Program to Perform Preorder Recursive Traversal of a Given Binary Tree
Difference Between Wait and Sleep in Java
Java Program to Implement Quick Sort Using Randomization
Hướng dẫn Java Design Pattern – Interpreter
Java Program to Check if an UnDirected Graph is a Tree or Not Using DFS
Map Interface trong java
Configuring a DataSource Programmatically in Spring Boot
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching
Lập trình đa luồng với CompletableFuture trong Java 8
Java Program to Perform Optimal Paranthesization Using Dynamic Programming
Sử dụng CyclicBarrier trong Java
Vấn đề Nhà sản xuất (Producer) – Người tiêu dùng (Consumer) và đồng bộ hóa các luồng trong Java
Multi Dimensional ArrayList in Java
Lớp Properties trong java
Java Program to Perform Left Rotation on a Binary Search Tree
Java Program to implement Bit Matrix
What is Thread-Safety and How to Achieve it?
Java IO vs NIO
Inheritance and Composition (Is-a vs Has-a relationship) in Java
Spring Security Form Login
Generic Constructors in Java
Java Program to Implement Stein GCD Algorithm
New Features in Java 8
Hướng dẫn Java Design Pattern – Singleton
Java Program to Implement CountMinSketch
Spring Boot - Hystrix
Java Program to Implement the Program Used in grep/egrep/fgrep
HttpClient 4 – Send Custom Cookie