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:

Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Feign – Tạo ứng dụng Java RESTful Client
Java Program to Implement Insertion Sort
Java Program to Implement Bellman-Ford Algorithm
Java Program to Implement PriorityQueue API
Java Program to Perform Sorting Using B-Tree
Java Program to Implement HashTable API
Validate email address exists or not by Java Code
DynamoDB in a Spring Boot Application Using Spring Data
Java Program to implement Dynamic Array
Quick Guide to Spring MVC with Velocity
Java Program to Implement Quick sort
Java Program to Implement Sorting of Less than 100 Numbers in O(n) Complexity
Java Program to Implement the RSA Algorithm
Java Program to Implement Aho-Corasick Algorithm for String Matching
Collect a Java Stream to an Immutable Collection
Java Program to Implement Disjoint Sets
Integer Constant Pool trong Java
Map to String Conversion in Java
Java Program to Perform Encoding of a Message Using Matrix Multiplication
Exploring the Spring Boot TestRestTemplate
Java Program to Implement Heap Sort Using Library Functions
Java Program to Perform Search in a BST
Spring Security Logout
Guide to UUID in Java
Java Program to Implement Direct Addressing Tables
Properties with Spring and Spring Boot
Java Program to Use Dynamic Programming to Solve Approximate String Matching
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Checking for Empty or Blank Strings in Java
Hướng dẫn Java Design Pattern – Factory Method
Spring Boot - Runners