Table of Contents
How to set random numbers in ava?
Home Java javaTutorial How to set random numbers in java

How to set random numbers in java

Jul 22, 2017 pm 03:19 PM
java Tutorial random number

java is a programming language used by programmers and developers. Many beginners have fallen into a simple question: how to set random numbers when developing using java? How to set random numbers in java? There are often places where random numbers need to be used. Don't worry, let's take a look at the detailed tutorial on setting random numbers in Java.

How to set random numbers in ava?

1. In j2se, we can use the Math.random() method to generate a random number. The generated random number is a double between 0-1. We can multiply it by a certain number. , for example, multiplied by 100, it is random within 100, which is not available in j2me.

2. The java.util package provides a Random class. We can create a new Random object to generate random numbers. It can generate random integers, random floats, random doubles, and random longs. This It is also a method of obtaining random numbers that we often use in j2me programs.

3. There is a currentTimeMillis() method in our System class. This method returns a number of milliseconds from 0:00:00 on January 1, 1970 to the present. The return type is long. We can use it as a random number, we can use it to modulo some numbers, and then limit it to a range

In fact, the third method above is also used in the default construction method of Random Method to generate random numbers

There are following instructions for the Random class in method two:

The java.util.Random class can be constructed in two ways: with seeds and without seeds

Without seed:

This method will return a random number, and the result will be different every time it is run

public class RandomTest {

public static void main(String[] args) {

 java.util.Random r=new java.util.Random();

 for(int i=0;i<10;i++){

System.out.println(r.nextInt());

 }

 }

With seed:

This kind method, no matter how many times the program is run, the return result is the same

public static void main(String[] args) {

java.util.Random r=new java.util.Random (10);

 for(int i=0;i<10;i++){

 System.out.println(r.nextInt());

 }

 }

 The difference between the two methods is

 (1) First, please open the Java Doc, we will see the description of the Random class:

 This Instances of this class are used to generate a stream of pseudo-random numbers. This class uses a 48-bit seed, which can be modified using the linear congruence formula (see The Art of Computer Programming, Volume 2, Volume 2 by Donald Knuth, page 3.2 .1 section).

If you create two Random instances with the same seed, and make the same sequence of method calls to each instance, they will generate and return the same sequence of numbers. In order to ensure the realization of this feature, we specify a specific algorithm for the Random class. For full portability of Java code, Java implementations must make class Random use all the algorithms shown here. But subclasses of the Random class are allowed to use other algorithms, as long as they conform to the general contract of all methods.

The Java Doc has explained the Random class very clearly, and our tests have also verified this.

 (2) If the seed number is not provided, the seed number of the Random instance will be the number of milliseconds of the current time. The number of milliseconds of the current time can be obtained through System.currentTimeMillis(). Opening the source code of the JDK, we can see this very clearly.

 /**

  * Creates a new random number generator. Its seed is initialized to

  * a value based on the current time:

  * Random() { this(System.currentTimeMillis()); }java.lang.System#currentTimeMillis()

  */

 public Random() { this(System.currentTimeMillis()); }

 Also:

 random Description of the nextInt(), nextInt(int n) method of the object:

int nextInt()

Returns the next pseudo-random number, which is uniformly distributed in the sequence of this random number generator int value.

int nextInt(int n)

Returns a pseudo-random number taken from this random number generator's sequence between 0 (inclusive) and the specified value (exclusive) uniformly distributed int values.

The above is the detailed content of How to set random numbers in java. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

See all articles