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 Cloud – Tracing Services with Zipkin
Introduction to Spring Cloud OpenFeign
Hướng dẫn Java Design Pattern – Abstract Factory
Java Program to Implement Leftist Heap
Luồng Daemon (Daemon Thread) trong Java
Một số tính năng mới về xử lý ngoại lệ trong Java 7
Hướng dẫn Java Design Pattern – Transfer Object
Hướng dẫn sử dụng Java Reflection
A Quick Guide to Using Keycloak with Spring Boot
Java – InputStream to Reader
Java Program to Implement HashTable API
Exploring the New Spring Cloud Gateway
Java Program to Generate Random Numbers Using Multiply with Carry Method
Java Program to Implement Bresenham Line Algorithm
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set
Java Program to Check the Connectivity of Graph Using DFS
Hashing a Password in Java
Spring Security OAuth2 – Simple Token Revocation
Java Program to Generate Random Numbers Using Probability Distribution Function
HTTP Authentification and CGI/Servlet
Spring Boot - Hystrix
Intersection of Two Lists in Java
How to Delay Code Execution in Java
Optional trong Java 8
Một số từ khóa trong Java
Spring Security with Maven
Introduction to Using Thymeleaf in Spring
Using a List of Values in a JdbcTemplate IN Clause
Java Program to Find Whether a Path Exists Between 2 Given Nodes
Java Copy Constructor
Returning Image/Media Data with Spring MVC
Spring Security OAuth Login with WebFlux