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:
Check if a String is a Palindrome in Java
Java Program to Implement Merge Sort Algorithm on Linked List
Hướng dẫn Java Design Pattern – Adapter
Java Program to Create a Random Graph Using Random Edge Generation
Spring Boot - Hystrix
Java Program to Implement Fisher-Yates Algorithm for Array Shuffling
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
Guide to the Java Clock Class
OAuth2 Remember Me with Refresh Token
Java Program to Implement Ternary Tree
Java Program to Implement Rope
Rest Web service: Filter và Interceptor với Jersey 2.x (P2)
Java Program to Implement Bellman-Ford Algorithm
Custom JUnit 4 Test Runners
Spring MVC Setup with Kotlin
StringBuilder vs StringBuffer in Java
Java String Conversions
LinkedHashSet trong java
Java Web Services – JAX-WS – SOAP
Java Program to Implement Bucket Sort
Java Program to Check whether Directed Graph is Connected using DFS
Java Program to Implement Sparse Matrix
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)
Java Program to Implement Patricia Trie
Hướng dẫn Java Design Pattern – Prototype
Java Program to Implement Hopcroft Algorithm
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
Spring Boot - Admin Client
Spring Cloud Connectors and Heroku
Java Program to Check Whether an Undirected Graph Contains a Eulerian Cycle
Java Program to Implement Counting Sort
Java – Combine Multiple Collections