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

时间:2018-09-30 20:06:55

标签: python matlab probability rainbowtable

这是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;

给定一个彩虹表,其中包含键空间N,大的无符号整数,长度为t的链和链数为m的情况下,它计算找到明文密码成功的概率。

示例运行:

calc_success_probability(80603140212, 2400, 40000000)

返回0.6055。

我很难将其转换为Python。在Python 3中,不再有最大整数,因此N不再是问题。我认为在计算中,我必须将所有内容强制设置为较大的浮点数,但我不确定。

我也不知道MATLAB中的运算顺序。我认为代码是这样说的:

创建大小为[1 .. 10]的数组,以便十个元素 用零初始化该数组的每个元素

在基于零的索引中,我认为这将是array[0 .. t-1],看起来MATLAB会将1用作第一个(第0个)索引。

然后将数组(基于0的索引)的第二个元素初始化为m

对于数组中的每个元素,将pos=1(从0开始的索引)到t-1

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

其中**是幂运算符。我认为幂在MATLAB中是^,因此N * (1 - (1-1/N)的{​​{1}}就像上面的幂。

然后设置一个指数。对于数组0到array[pos-1]中的每个元素:   指数是指数+ 1

返回概率= t-1的幂次;

我的Python代码如下所示,并且不起作用。我不知道为什么,但是可能是因为我对MATLAB或Python的理解不够,或者我以某种方式读错了数学,而MATLAB中发生的事情并不是我所期望的,即我的操作顺序和/或类型错误,无法正常工作,而我在这些方面缺少某些东西...

1 - (1 - 1/N)

1 个答案:

答案 0 :(得分:2)

注意括号!您已经翻译了:

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {

    private Map<Integer, Integer> mSpinnerSelectedItem = new HashMap<Integer, Integer>();


//this context we will use to inflate the layout
    //Remove this..Please
    // CheckBox checkBox;

    //private Context mCtx;
    private SearchableSpinner spinner;

    //we are storing all the products in a list
    private List<Product> productList;

    private Activity create;

    public ProductAdapter(Activity activity) {
        create = activity;
    }


    //getting the context and product list with constructor
    public ProductAdapter(Activity activity, List<Product> productList) {
        // this.mCtx = mCtx;
        create = activity;
        this.productList = productList;
    }

    @Override
    public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //inflating and returning our view holder
        LayoutInflater inflater = LayoutInflater.from(create);
        View view = inflater.inflate(R.layout.layout_products, null);
        return new ProductViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ProductViewHolder holder, final int position) {
        // //getting the product of the specified position


        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(create, R.layout.item_spinner_layout,
                Product.getSpinnerItemsList());
        spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        holder.spinner.setAdapter(spinnerArrayAdapter);

        holder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int mPosition, long id) {
                mSpinnerSelectedItem.put(position, mPosition);

                TextView mTextView = view.findViewById(R.id.mSpinnerText);
           /* Toast.makeText(create, "Selected Item: " + mTextView.getText().toString(), Toast.LENGTH_LONG).show();
            Log.e("***************", "Selected Item: " + mTextView.getText().toString());*/


            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


        //binding the data with the viewholder views
        if (mSpinnerSelectedItem.containsKey(position)) {
            holder.spinner.setSelection(mSpinnerSelectedItem.get(position));
        }


        holder.getView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(create);


                // set title
                alertDialogBuilder.setTitle("Delete Item");

                // set dialog message
                alertDialogBuilder
                        .setMessage("Are you sure you want to delete this item?")
                        .setCancelable(false)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // if this button is clicked, close
                                // current activity


                                holder.checkBox.setChecked(false);
                                holder.spinner.setSelection(0);

                                productList.remove(position);
                                notifyItemRemoved(position);

                                Toast.makeText(create, "Item removed.", Toast.LENGTH_LONG).show();


                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // if this button is clicked, just close
                                // the dialog box and do nothing
                                dialog.cancel();
                            }
                        });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();

            }
        });





    }


    @Override
    public int getItemCount() {
        return productList.size();
    }


    class ProductViewHolder extends RecyclerView.ViewHolder {

        SearchableSpinner spinner;
        EditText editText;
        TextView textView5;
        CheckBox checkBox;
        LinearLayout linearLayout;
        View rootView;


        public ProductViewHolder(View itemView) {
            super(itemView);

            spinner = itemView.findViewById(R.id.spinner);
            editText = itemView.findViewById(R.id.editText);
            textView5 = itemView.findViewById(R.id.textView5);
            checkBox = itemView.findViewById(R.id.checkBox);
            rootView = itemView.findViewById(R.id.linearLayout);


            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    // makes the set disappear when checkbox is ticked.
                    if(isChecked){

                        checkBox.setChecked(false);
                        spinner.setSelection(0);

                        productList.remove(getAdapterPosition());
                        notifyItemRemoved(getAdapterPosition());



                        Toast.makeText(create, "Done!", Toast.LENGTH_LONG).show();
                    }

                }
            });



        }

        public View getView() {
            return rootView;
        }

    }

}

对此:

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

我已经整理了所有内容,因此您可以更好地了解问题出在哪里。您已将括号移到错误的位置。

应该是:

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

类似地,

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

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

应该是

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