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 problem of equal probability

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

Share

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

This article introduces the relevant knowledge of "how to solve the problem of equal probability". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In solving algorithm problems, we often encounter problems that require equal probability. Leetcode 470. Use Rand7 () to implement Rand10 () as an example.

The existing method rand7 can generate uniform random integers in the range of 1 to 7. Try to write a method rand10 to generate uniform random integers in the range of 1 to 10.

⚠️ does not discuss the optimal solution, but only discusses the problem of seeing equal probability in the algorithm. We should first think of changing it to binary system. The idea is to convert equal probability into equal probability 0 and 1, and then increase the number of digits to deal with other numbers of equal probability. To disassemble the above question, we use Rand7 to convert it into Rand2. Let the return results of Rand2 appear equal to 0 and 1, and we can use 4-digit binary numbers to generate numbers containing 0-15. Discard 10: 15 and keep 0 to 9. As a result, adding 1 is a random number from 1 to 10.

The first step is to convert the binary function

The result of Rand7 () is that there is an equal probability of 1, 2, 3, 4, 5, 5, 6, 1, 2, 4, 5, 5, 6, 4, 4, 5, 5, 6, 5, 5, 5, 5, 6, 4, 4, 5, 5, 6, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 6, 6, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 6, 4, 4, 5, 6, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,

Declare function rand7 (): number function Rand2 (): number {return Rand7 () > 3? 1: 0}

Now that we have the transition function Rand2, then we use random generation of 4-digit binary numbers, and I get a function that generates 0-15 equally.

Function Rand15 (): number {return Rand2 () * 2 * 2 + Rand2 () * 2 * 2 + Rand2 () * 2 + Rand2 ()}

The above code is a little stupid, we use the shift method to optimize the left shift operator is binary carry.

Function Rand15 (): number {return (rand2 ()

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