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 Sort an Array of 10 Elements Using Heap Sort Algorithm
Java Program to Implement Circular Doubly Linked List
Spring Data Java 8 Support
Apache Commons Collections OrderedMap
Hướng dẫn Java Design Pattern – Decorator
Java Program to Compare Binary and Sequential Search
Spring Boot - Flyway Database
Java Program to Perform String Matching Using String Library
Feign – Tạo ứng dụng Java RESTful Client
Logging a Reactive Sequence
@DynamicUpdate with Spring Data JPA
Java Program to Implement Kosaraju Algorithm
Explain about URL and HTTPS protocol
The Difference Between map() and flatMap()
Java List UnsupportedOperationException
Database Migrations with Flyway
Spring @RequestMapping New Shortcut Annotations
The DAO with JPA and Spring
The Spring @Controller and @RestController Annotations
The Basics of Java Security
A Custom Data Binder in Spring MVC
Guide to DelayQueue
Spring Boot - Code Structure
Tính trừu tượng (Abstraction) trong Java
Java Program to Implement Fisher-Yates Algorithm for Array Shuffling
Spring Cloud Connectors and Heroku
Using the Not Operator in If Conditions in Java
“Stream has already been operated upon or closed” Exception in Java
Truyền giá trị và tham chiếu trong java
Create a Custom Auto-Configuration with Spring Boot
Quick Guide on Loading Initial Data with Spring Boot
Spring MVC Setup with Kotlin