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:
Java Program to Test Using DFS Whether a Directed Graph is Strongly Connected or Not
Java Program to Implement Ternary Heap
Guide to the Synchronized Keyword in Java
A Quick Guide to Using Keycloak with Spring Boot
Java Program to Implement Heap Sort Using Library Functions
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
Java Program to Implement Sorted Circularly Singly Linked List
A Guide to EnumMap
Custom HTTP Header with the HttpClient
Apache Camel with Spring Boot
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Java Program to Implement Graph Structured Stack
ExecutorService – Waiting for Threads to Finish
Introduction to PCollections
Introduction to Spring Cloud Rest Client with Netflix Ribbon
Java Program to Implement Binary Heap
Java Program to Find Nearest Neighbor for Static Data Set
Quick Guide to Spring Bean Scopes
Using JWT with Spring Security OAuth (legacy stack)
Request Method Not Supported (405) in Spring
Intro to Inversion of Control and Dependency Injection with Spring
Converting between an Array and a List in Java
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
CharSequence vs. String in Java
Java Program to Implement Randomized Binary Search Tree
Spring Boot - Creating Docker Image
Hướng dẫn Java Design Pattern – Flyweight
Hướng dẫn Java Design Pattern – Command
Tạo ứng dụng Java RESTful Client với thư viện Retrofit
Apache Commons Collections SetUtils
Wiring in Spring: @Autowired, @Resource and @Inject
OAuth2 Remember Me with Refresh Token