Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the seed problem in Java Random class

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "how to solve the seed problem in the Java Random class". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to solve the seed problem in the Java Random class" can help you solve your doubts.

It can be said that in today's computer language, as well as ordinary users here, there is no way to get real random numbers. Real random numbers are now available only in quantum computers.

So the random number we are talking about now can also be called pseudo-random number, which is the result obtained by an algorithm and combined with the next regular number which is constantly changing (such as time).

We call this kind of data which uses the set time of the algorithm as the benchmark for the initial acquisition of random numbers.

There are two ways to create random numbers in Java:

The second way is to create Random objects directly.

Random random = new Random ()

This method is called at the bottom to get the seed:

The second is to spread the seeds you have created:

Random random = new Random (5)

In the source code, we can directly see that the data type of the seed is long, which is long shaping.

So we now have a way to get the seed it generated when it first generated the Random object, and then pass the seed back into the object that created the Random. Is it possible to get the same random number as above?

Let's implement it in java language:

Import java.util.Random;public class Demo02 {public static void main (String [] args) {Random random = new Random (5); / / pass 5 as a seed to System.out.println here ("first call:"); for (int I = 0; I < 5; iTunes +) {System.out.print (random.nextInt (10) + "\ t") / / get a random number within 10 (excluding 10)} random.setSeed (5); / / pass seed 5 into the seed of Random ("\ n2nd call:"); for (int I = 0; I < 5; iTunes +) {System.out.print (random.nextInt (10) + "\ t");}

It is obvious here that this random number is not random at all. Twice, as long as the seeds are the same, you can return the same result.

Here I use python language to implement this function again (mainly because there is no method in java that can output the current seed). Use python to have a better understanding.

In python, I will write more detailed comments, the details will not be difficult to understand.

Import random # Import random library x = random.getstate () # to get the seed print in the current random ('first loop:') # input statement for i in range (5): # for loop for i in range (5) = = "for (int I = 0; I < 5) Python +) # there are no curly braces to wrap around in python, but you should pay attention to locking. A lock is a whole print (random.randint (1,10), end='\ t') # random.randint (1,10) generates a random number of 1x 10 (excluding 10), # end='\ t 'indicates that the current print is not finished, and there is a tab key. Print ('\ nsecond loop') random.setstate (x) # re-pass the seeds obtained above into for i in range (5): # here, as above, cycle out print (random.randint (1,10), end='\ t') five times

After reading this, the article "how to solve the seed problem in the Java Random category" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report