百层高楼玻璃碎

这其实是求最坏情况下的最优解

一些分析可以看:https://www.zhihu.com/question/31855632
这幅图其实已经讲明了:
image.png

三人三鬼把河过

链接
image.png

链接
image.png

概率为峰-1

原文链接:https://blog.csdn.net/weixin_41888257/article/details/107589772

image.png
image.png

  1. int random_0_1()
  2. {
  3. int i = RANDOM();
  4. int j = RANDOM();
  5. int result;
  6. while (true)
  7. {
  8. if (i == 0 && j == 1)
  9. {
  10. result = 0;
  11. break;
  12. }
  13. else if (i == 1 && j == 0)
  14. {
  15. result = 1;
  16. break;
  17. }
  18. else
  19. continue;
  20. }
  21. return result;
  22. }

image.png

概率为峰-2

image.png

  1. import random
  2. class Solution:
  3. def random_index(self):
  4. # """随机变量的概率函数"""
  5. # 参数rate为list<int>
  6. # 返回概率事件的下标索引
  7. # 这是一个10%概率产生0,90%概率产生1的生成器
  8. rate = [1, 9]
  9. start = 0
  10. index = 0
  11. randnum = random.randint(1, sum(rate))
  12. for index, scope in enumerate(rate):
  13. start += scope
  14. if randnum <= start:
  15. break
  16. return index
  17. def Rand1(self):
  18. # 1. 等概率生成 0,1
  19. i1 = self.random_index()
  20. i2 = self.random_index()
  21. if i1 == 0 and i2 == 1:
  22. return 1
  23. elif i1 == 1 and i2 == 0:
  24. return 0
  25. else:
  26. return self.Rand1()
  27. return -1
  28. def Rand2(self, n: int):
  29. # 2. 以 1/N 的概率返回 1~N 之间的数
  30. # int(res, 2) 表示把 str 类型的二进制转成十进制 int('101',2) == 5
  31. k = len(bin(n)[2:]) # k = 1 + np.log2(n)
  32. print("k:", k) # 10
  33. res = ''
  34. for i in range(k):
  35. res += str(self.Rand1())
  36. if int(res, 2) > n:
  37. return self.Rand2(n)
  38. else:
  39. return int(res, 2)
  40. def Rand3(self, n: int):
  41. # 3. 以 1/N 的概率返回 1~N 之间的数
  42. # (1<<i) 表示 1 * 2^i
  43. import numpy as np
  44. result = 0
  45. k = 1 + np.log2(n)
  46. print("k:", int(k)) # 10
  47. for i in range(0, int(k)):
  48. if self.Rand1() == 1:
  49. result |= (1<<i)
  50. if result > n:
  51. return self.Rand3(n)
  52. return result
  53. if __name__ == '__main__':
  54. solution = Solution()
  55. rand1 = []
  56. for i in range(20):
  57. rand1.append(solution.Rand1())
  58. print(rand1)
  59. rand2 = []
  60. for i in range(10):
  61. rand2.append(solution.Rand2(999))
  62. print(rand2)
  63. rand3 = []
  64. for i in range(10):
  65. rand3.append(solution.Rand3(999))
  66. print(rand3)

概率为峰-3

image.png
image.png

  1. int rand7()
  2. {
  3. int x = 0;
  4. do
  5. {
  6. x = 5 * (rand5() - 1) + rand5();
  7. }while(x > 21);
  8. return 1 + x % 7;
  9. }

image.png
LeetCode-rand7生成rand10

  1. public int rand10() {
  2. // 首先得到一个数
  3. int num = (rand7() - 1) * 7 + rand7();
  4. // 只要它还大于40,那你就给我不断生成吧
  5. while (num > 40)
  6. num = (rand7() - 1) * 7 + rand7();
  7. // 返回结果,+1是为了解决 40%10为0的情况
  8. return 1 + num % 10;
  9. }

image.png
image.png

  1. /**
  2. * The rand7() API is already defined in the parent class SolBase.
  3. * public int rand7();
  4. * @return a random integer in the range 1 to 7
  5. */
  6. class Solution extends SolBase {
  7. public int rand10() {
  8. while (true){
  9. int num = (rand7() - 1) * 7 + rand7();
  10. // 如果在40以内,那就直接返回
  11. if(num <= 40) return 1 + num % 10;
  12. // 说明刚才生成的在41-49之间,利用随机数再操作一遍
  13. num = (num - 40 - 1) * 7 + rand7();
  14. if(num <= 60) return 1 + num % 10;
  15. // 说明刚才生成的在61-63之间,利用随机数再操作一遍
  16. num = (num - 60 - 1) * 7 + rand7();
  17. if(num <= 20) return 1 + num % 10;
  18. }
  19. }
  20. }

image.png