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:
The XOR Operator in Java
Redirect to Different Pages after Login with Spring Security
Converting Between Byte Arrays and Hexadecimal Strings in Java
Java Program to Check the Connectivity of Graph Using BFS
Validate email address exists or not by Java Code
Converting Strings to Enums in Java
HttpClient Basic Authentication
Spring Boot Security Auto-Configuration
Java Program to Implement Efficient O(log n) Fibonacci generator
A Guide to Spring Cloud Netflix – Hystrix
Java Program to Implement Hash Tables Chaining with List Heads
JWT – Token-based Authentication trong Jersey 2.x
Java Program to implement Priority Queue
Java Program to Implement Min Heap
Java Program to Check if a Given Graph Contain Hamiltonian Cycle or Not
Spring Cloud – Securing Services
Java Program to Generate a Sequence of N Characters for a Given Specific Case
Base64 encoding và decoding trong Java 8
Java Program to Implement Gale Shapley Algorithm
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Guide to @JsonFormat in Jackson
Spring Boot - Twilio
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
A Guide To UDP In Java
Spring REST API + OAuth2 + Angular
Comparing Long Values in Java
Java Program to Implement Sorted Circularly Singly Linked List
Spring Boot - Admin Client
How to Kill a Java Thread
Unsatisfied Dependency in Spring
Giới thiệu HATEOAS
Immutable Objects in Java