OpenCV函数解析之theRNG()

OpenCV中的函数-theRNG(),到底什么意思呢?

源码中关于theRNG()的注释如下:

/** @brief Returns the default random number generator.

The function cv::theRNG returns the default random number generator. For each thread, there is a
separate random number generator, so you can use the function safely in multi-thread environments.
If you just need to get a single random number using this generator or initialize an array, you can
use randu or randn instead. But if you are going to generate many random numbers inside a loop, it
is much faster to use this function to retrieve the generator and then use RNG::operator _Tp() .
@sa RNG, randu, randn
*/

翻译如下:

/** 返回默认的随机数生成器。

函数cv::theRNG返回默认随机数生成器。
对于每个线程,有一个单独的随机数生成器,这样您就可以在多线程环境中安全地使用该函数


如果您只需要使用这个生成器获得一个随机数或初始化一个数组,您可以使用randu或randn代替。但是如果你想在一个循环中产生很多随机数,那么使用此函数检索生成器,然后再使用RNG::operator _Tp(),这样做会快很多。
@sa RNG, randu, randn
* /

大概意思就是:使用这个函数可以直接返回一个当前线程的随机数生成器。

如:RNG &rng = theRNG();  //用其引用来接收theRNG函数返回的随机数生成器(RNG就是随机数生成器的类)

 

RNG& rng=theRNG();

double x= (double)rng;
float y= (float)rng;
int z= (int)rng;

double x = rng.uniform((double)0,(double)255);
float y = rng.uniform(0.f,255.f);
int z = rng.uniform((int)0, (int)255 );

 

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值