简体   繁体   English

将MATLAB代码转换为Python:Python类型和操作顺序

[英]Converting MATLAB code to Python: Python types and order of operations

This is a MATLAB function from the author of RainbowCrack: 这是RainbowCrack作者的MATLAB函数:

function ret = calc_success_probability(N, t, m)
arr = zeros(1, t - 1);
arr(1) = m;
for i = 2 : t - 1
    arr(i) = N * (1 - (1 - 1 / N) ^ arr(i - 1));
end

exp = 0;
for i = 1 : t - 1
    exp = exp + arr(i);
end

ret = 1 - (1 - 1 / N) ^ exp;

It calculates the probability of success in finding a plaintext password given a rainbow table with keyspace N , a large unsigned integer, chain of length t , and number of chains m . 给定一个彩虹表,该彩虹表具有键空间N ,一个大的无符号整数,长度为t的链和链的数目为m ,它计算找到明文密码成功的概率。

A sample run: 运行示例:

calc_success_probability(80603140212, 2400, 40000000)

Returns 0.6055. 返回0.6055。

I am having difficulty converting this into Python. 我很难将其转换为Python。 In Python 3, there is no max integer anymore, so N isn't an issue. 在Python 3中,不再有最大整数,因此N不再是问题。 I think in the calculations I have to force everything to a large floating point number, but I'm not sure. 我认为在计算中,我必须将所有内容强制设置为较大的浮点数,但我不确定。

I also don't know the order of operations in MATLAB. 我也不知道MATLAB中的运算顺序。 I think the code is saying this: 我认为代码是这样说的:

Create array of size [1 .. 10] so ten elements Initialize every element of that array with zero 创建大小为[1 .. 10]的数组,以便十个元素用零初始化该数组的每个元素

In zero-based indexing, I think this would be array[0 .. t-1] , it looks like MATLAB uses 1 as the first (0'th) index. 在基于零的索引中,我认为这将是array[0 .. t-1] ,看起来MATLAB使用1作为第一个(第0个)索引。

Then second element of array (0-based indexing) initialized to m . 然后将数组的第二个元素(基于0的索引)初始化为m

For each element in array, pos=1 (0-based indexing) to t-1 : 对于数组中的每个元素, pos=1 (从0开始的索引)到t-1

array[pos] = N * (1 - (1 - 1/N) ** array[pos-1]

Where ** is the power operator. 其中**是幂运算符。 I think power is ^ in MATLAB, so N * (1 - (1-1/N) to the array[pos-1] power is like that above. 我认为MATLAB中的功效为^ ,因此对array[pos-1] N * (1 - (1-1/N)

Then set an exponent. 然后设置一个指数。 For each element in array 0 to t-1 : exponent is exponent + 1 对于数组0到t-1每个元素:指数为指数+ 1

return probability = 1 - (1 - 1/N) power of exp; 返回概率= exp的1 - (1 - 1/N)乘方;

My Python code looks like this, and doesn't work. 我的Python代码看起来像这样,并且不起作用。 I can't figure out why, but it could be that I don't understand MATLAB enough, or Python, both, or I'm reading the math wrong somehow and what's going on in MATLAB is not what I'm expecting, ie I have order of operations and/or types wrong to make it work and I'm missing something in those terms... 我不知道为什么,但是可能是因为我对MATLAB或Python的理解不够,或者我以某种方式读错了数学,而MATLAB中发生的事情并不是我所期望的,即我的操作顺序和/或类型错误,无法正常工作,而我在这些方面缺少一些东西...

def calc_success_probability(N, t, m):

    comp_arr = []

    # array with indices 1 to t-1 in MATLAB, which is otherwise 0 to t-2???
    # range with 0, t is 0 to t excluding t, so t here is t-1, t-1 is up
    # to including t-2... sounds wrong...
    for i in range(0, t-1):
        # initialize array
        comp_arr.append(0)

    print("t = {0:d}, array size is {1:d}".format(t, len(comp_arr)))

    # zero'th element chain count
    comp_arr[0] = m

    for i in range(1, t-1):
        comp_arr[i] = N * (1 - (1 - 1 / N)) ** comp_arr[i-1]

    final_exp = 0
    for i in range(0, t-1):
        final_exp = final_exp + comp_arr[i]

    probability = (1 - (1 - 1 / N)) ** final_exp

    return probability

Watch your brackets! 注意你的括号! You have translated this: 您已经翻译了:

arr(i)      = N * (   1 - ( 1 - 1 / N )     ^  arr(i - 1)     );

to this: 对此:

comp_arr[i] = N * (   1 - ( 1 - 1 / N )   ) ** comp_arr[i-1]

I've lined up everything so you can better see where it goes wrong. 我已经整理了所有内容,以便您可以更好地了解问题所在。 You've moved a bracket to the wrong location. 您已将支架移到错误的位置。

It should be: 它应该是:

comp_arr[i] = N * (   1 - ( 1 - 1 / N )     ** comp_arr[i-1]  )

Similarly, 同样,

ret = 1 - (1 - 1 / N) ^ exp;

is not the same as 与...不同

probability = (1 - (1 - 1 / N)) ** final_exp

This should be 这应该是

probability = 1 - (1 - 1 / N) ** final_exp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM