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:
Date Time trong Java 8
Java Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
Shuffling Collections In Java
Spring Data MongoDB Transactions
Từ khóa static và final trong java
Using JWT with Spring Security OAuth (legacy stack)
Using Spring @ResponseStatus to Set HTTP Status Code
Wiring in Spring: @Autowired, @Resource and @Inject
Java Program to Implement Hash Tables with Quadratic Probing
Using a Spring Cloud App Starter
Feign – Tạo ứng dụng Java RESTful Client
Java Program to Implement Coppersmith Freivald’s Algorithm
Java Program to Find Whether a Path Exists Between 2 Given Nodes
A Guide to ConcurrentMap
Java Program to Generate N Number of Passwords of Length M Each
Setting the Java Version in Maven
Intro to Spring Boot Starters
Câu lệnh điều khiển vòng lặp trong Java (break, continue)
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Receive email by java client
Sử dụng Fork/Join Framework với ForkJoinPool trong Java
Introduction to Spring Boot CLI
Chuyển đổi giữa các kiểu dữ liệu trong Java
Java Program to Implement Control Table
HttpClient Connection Management
Convert Hex to ASCII in Java
Java Program to Implement Ford–Fulkerson Algorithm
Java Program to Find the Mode in a Data Set
Assertions in JUnit 4 and JUnit 5
Java Program to Represent Graph Using Adjacency List
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
Java Program to Implement Interval Tree