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:
Calling Stored Procedures from Spring Data JPA Repositories
Tạo chương trình Java đầu tiên sử dụng Eclipse
Java Program to Compute DFT Coefficients Directly
Composition, Aggregation, and Association in Java
Java Program to Implement Max Heap
StringBuilder vs StringBuffer in Java
Vector trong Java
Working with Tree Model Nodes in Jackson
Java Program to Implement Hash Tables
Java Program to Implement Graph Structured Stack
Java Program to Implement DelayQueue API
Guide to Selenium with JUnit / TestNG
Java Program to Generate a Sequence of N Characters for a Given Specific Case
OAuth 2.0 Resource Server With Spring Security 5
Java Program to Perform integer Partition for a Specific Case
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
Java Program to Generate Date Between Given Range
Introduction to Thread Pools in Java
Spring JDBC
Giới thiệu luồng vào ra (I/O) trong Java
Entity To DTO Conversion for a Spring REST API
Giới thiệu về Stream API trong Java 8
Từ khóa throw và throws trong Java
Jackson – JsonMappingException (No serializer found for class)
Java CyclicBarrier vs CountDownLatch
Remove All Occurrences of a Specific Value from a List
Spring Webflux and CORS
Java Multi-line String
HTTP Authentification and CGI/Servlet
An Example of Load Balancing with Zuul and Eureka
A Quick Guide to Spring Cloud Consul
Java Program to Implement LinkedHashSet API