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 get the number in a certain range of integers in java

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly introduces "how to get integers within a certain range of numbers in java". In daily operation, I believe that many people have doubts about how to obtain integers within a certain range of numbers in java. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "how to get integers within a certain range in java". Next, please follow the editor to study!

The function of random number

Random number is a very common thing in our daily life, no matter data encryption or rolling dice, the purpose is to make the result uncertain, no matter what result there is a certain probability. But how exactly do random numbers come into being in our computers?

Principle of generating true and false random numbers

Most programs and programming languages do generate random numbers are pseudo-random numbers, such as C language, often use linear congruence or pseudorandom numbers generated by a seed, there are certain rules to follow. The same is true of our commonly used high-level programming language java. For example, get the digital source code within a certain range of integers in java:

Final int internalNextInt (int origin, int bound) {

If (origin

< bound) { int n = bound - origin; if (n >

0) {

Return nextInt (n) + origin

}

Else {/ / range not representable as int

Int r

Do {

R = nextInt ()

} while (r

< origin || r >

= bound)

Return r

}

}

Else {

Return nextInt ()

}

}

This proves that if you know these seeds, or based on the random numbers that have been generated, you can predict the next possible number.

Speaking of which, let's think about it the other way around, is it true that real random numbers don't exist? For example, banks often remind people not to use their own birthdays, certain digits in their ID card numbers or their own mobile phone numbers, because it may be cracked at any time, so we should try our best to use some random numbers, such as an intersection. In the first minute, several people walked as the first bit of your password, and then arranged in turn, this number is random. Similarly, if our computers rely solely on programs, there may be no real random numbers, if computer software and external hardware are combined. For example, collect signals such as daily CPU speed and temperature, and even the frequency of someone tapping the keyboard, disk write speed, and so on. This produces a "pure" random number.

There is also an entropy pool in the unix operating system, which collects some random values through the entropy pool, which produces real random numbers.

At this point, the study of "how to get numbers within a certain range of integers in java" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Servers

Wechat

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

12
Report