How do you design a rand7 function

本文探讨了如何利用rand5函数生成rand7函数的方法。通过分析确定性解决方案的局限性,并提供了一种通过循环调用rand5来实现均匀分布rand7的算法。

How do you design a rand7 function using a rand5 function?

My solution is:

def rand7():
     
     n = rand5() + rand5() + rand5() + rand5() + rand5() + rand5() + rand5() + rand5()
     return n%8

Firstly, is the correct? Second, either way provide additional ways of accomplishing this. Thanks!

Update: 10000000 simulations resulted in the following results

0  had count  1250972  Chance:  12.50972%
1  had count  1251284  Chance:  12.51284%
2  had count  1248768  Chance:  12.48768%
3  had count  1251610  Chance:  12.5161%
4  had count  1250475  Chance:  12.50475%
5  had count  1249196  Chance:  12.49196%
6  had count  1249427  Chance:  12.49427%
7  had count  1248268  Chance:  12.48268%
5 Answers
Gayle Laakmann McDowell 
Gayle Laakmann McDowellAuthor of Cracking the Coding Interview
Rand7 is supposed to turn one of 7 numbers (0 through 6 OR 1 through 7). Rand5 returns one of 5 numbers. For my explanation, I've assumed that rand7 returns 0 through 6.

There is no deterministic way of doing this [see explanation below]. By "deterministic" we mean that there is no way of doing this such that you can GUARANTEE that the program will return within X calls to rand. So, no, your solution does not work since it is deterministic.

How can you solve it? You'll have to do it with a loop that could, theoretically, loop for a very long time.

Here's one simple way:
  1. Call rand5 until you get a number between 0 and 3 (that is, anything but 4). Set b0 (bit 0) to that number % 2. So b0 is 0 if rand5 was 0 or 2, and 1 otherwise.
  2. Do the same for bit 1 and bit 2.
  3. We now have a 3 bit number -- which will be a number between 0 and 7.
  4. If that number is a 7, start over. Else, return.

In other words, we're doing this:
int rand2() {
    int x = rand5();
    if x == 4 return rand2(); // restart
    else return x % 2;
}

int rand7() {
     int x = rand2() * 4 + rand2() * 2 + rand2();
     if (x == 7) return rand7(); // restart
     else return x;
}


Why there's no deterministic solution
Suppose there were a deterministic way of doing this. This means that you are guaranteeing me that within X calls  max to rand5, you can randomly generate a number between 0 and 6 with  equal probability.

Wonderful. Okay.

Observe that:
  1. If you run the rand7 program twice, and you get the same results both times for each rand5 call, you'll get the same result for the rand7 program.
  2. Some "paths" through the program might call rand5 fewer than X times. However, for the purposes we can treat this as still calling rand5 X times, where it just doesn't use those results.

So, now, we basically have a rand7 program that calls rand5  exactly X times. The results of those calls will determine what the value for rand7.

You can treat the sequence of results for rand5 as a string if you'd like. So, 013 means to get a 0 on the first call, then a 1, then a 3.

How many strings could you make? Well, X slots. 5 possibilities for each slot. So, there are 5^X slots.

Following me so far? Excellent!

Now then. I'd like you to divide those 5^X strings into 7  equal buckets.

What, you can't do that? Because 7 doesn't go into 5^X equally?

Oh. 

Guess your deterministic rand7 program isn't so equally random after all.
内容概要:本文介绍了一个针对电力系统连锁故障传播路径的N-k多阶段双层优化及故障场景筛选模型,该模型基于混合整数线性规划(MILP)方法构建,旨在全面评估电力系统在遭受多重故障时的脆弱性与恢复能力。通过引入故障传播路径的概念,模型能够动态模拟故障在电网中的逐级扩散过程,并结合多阶段优化策略,实现对关键故障场景的有效识别与优先排序。整个框架不仅考虑了初始故障元件的选取,还涵盖了后续因潮流转移引发的级联跳闸行为,从而提升了风险评估的准确性与时效性。该研究已在Matlab平台上完成代码实现,具备良好的可复现性和工程应用价值,适用于提升现代电网的安全防御水平。; 适合人群:电力系统、能源安全及相关领域的科研人员、高校研究生以及从事电网规划与运行管理的工程技术人员。; 使用场景及目标:①用于电力系统安全评估中识别最危险的N-k故障组合;②支撑电网应急预案制定与薄弱环节改造;③作为学术研究中关于级联故障建模与优化求解的教学与验证工具;④服务于智能电网背景下抵御蓄意攻击或极端事件的风险防控决策。; 阅读建议:建议读者结合Matlab代码深入理解模型的数学 formulation 与求解流程,重点关注目标函数设计、约束条件构建及双层优化结构的实现逻辑,同时可通过调整系统参数和故障设定进行仿真对比分析,以掌握不同因素对连锁故障演化的影响规律。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值