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 does Java implement a function so that the probability of return is the same

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article "Java how to achieve a function, so that the probability of return is the same" most people do not understand, so the editor summed up the following content, detailed, clear steps, with a certain reference value, I hope you can get something after reading this article, let's take a look at this "Java how to achieve a function, so that the probability of return is the same" article.

Topic: give a function that returns 0 and 1, and the probability is p and 1. Please implement a function so that the probability of returning 01 is the same.

Idea: if you call the underlying random function twice in a row, there are four results:

1, 00 probability is pumped

2. The probability of 11 is (1m / p) * (1m / p)

The probability of 3 and 10 is (1m / p) * p

4. The probability of 01 is p * (1murp). It can be seen that the probability of result 3 and result 4 are equal.

Public class SameProbability {

/ * *

* the probability of generating 0 is p, and the probability of generating 1 is 1merp.

*

* the probability of generating 0 1 is p (1murp)

* the probability of generating 1 0 is (1murp) p

* is equal.

, /

Public static int getZeroOrOneSameProbability () {

Random ra = new Random ()

While (true) {

Int I = ra.nextInt (10)

Int j = ra.nextInt (10)

If (I = = 0 & j = = 1) {

Return 1

} else if (I = = 1 & & j = = 0) {

Return 0

} else {

Continue

}

}

}

Public static void main (String [] args) {

Int result = getZeroOrOneSameProbability ()

System.out.println (result)

}

}

The above is about the content of this article on "how to implement a function in Java so that the probability of return is the same". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please 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

Internet Technology

Wechat

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

12
Report