Matlab rand和C ++ rand() [英] Matlab rand and c++ rand()

查看:110
本文介绍了Matlab rand和C ++ rand()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将matlab代码移植到c ++中,并在matlab中找到rand的用法. matlab的rand函数和c ++ rand()函数是否相同? 如果没有,那么在C ++或opencv中是否有用于matlab rand的内置函数?

解决方案

鉴于C ++标准仅宽松地定义了rand(),您可以断言它等同于matlab函数. /p>

幸运的是,C ++ 11现在具有一套生成器,这些生成器在标头<random>中得到了 的标准化.例如,std::mt19937是梅森扭曲器生成器,而std::minstd_rand是线性同余生成器,允许您配置序列系数.

我的猜测是Matlab生成器可以与后者一起复制.有关特定的详细信息,请参考Matlab文档.幸运的是,测试您的代码非常简单.

I am trying to port a matlab code into c++ and found the usage of rand in matlab . Is matlab's rand function and c++ rand() function same? If not, is there any inbuilt function for matlab's rand in C++ or opencv?

解决方案

Given that the C++ Standard only loosely defines rand(), you can not assert that it is equivalent to the matlab function.

Fortunately, C++11 now has a suite of generators which are standardised in the standard include header <random>. For example, std::mt19937 is a Mersenne twister generator and std::minstd_rand is a linear congruential generator which allows you to configure the sequence coefficients.

My guess is that the Matlab generator can be replicated with this latter one. Refer to the Matlab docs for the specific details. Fortunately, testing your code will be trivial.

这篇关于Matlab rand和C ++ rand()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆