简体   繁体   English

计算C ++的随机数

[英]Calculating a Random for C++

This is probably a super easy question, but I just wanted to make 10000% sure before I did it. 这可能是一个非常容易的问题,但是我只是想在做之前确定10000%。 Basically Im doing a formula for a program, it takes some certain values and does things when them.....etc.. 基本上,我为程序编写公式,它采用某些特定值,并在它们执行操作时执行.....等。

Anyways Lets say I have some values called: 无论如何,可以说我有一些值叫做:

N
Links_Retrieved
True_Links
True_Retrieved.

I also have a % "scalar" ill call it, for this example lets say the % scalar is 10%. 我也有一个%“ scalar”不适称呼,在这个示例中,%scalar为10%。

Links Retrieved is ALWAYS half of N, so that's easy to calculate. 检索的链接总是N的一半,因此很容易计算。 BUT I want True_Links to be ANYWHERE from 1-10% of Links_Retrieved. 但是我希望True_Links可以从Links_Retrieved的1-10%中变为任意。

Then I want True_Retrieved to be anywhere from The True_Links to 15% of Links_Retrieved. 然后,我希望True_Retrieved介于True_Links到Links_Retrieved的15%之间。 How would I do this? 我该怎么做? would it be something like 会不会像

True_Link=(((rand()%(Scalar(10%)-1))+1)/100);

? I would divide by 100 to get the "percent" value IE .1 so it's be anywhere from .01 to .1? 我将除以100以获得IE .1的“百分比”值,因此它的范围是.01到.1?

and to do the True_retrieved it'd be 并要做True_retrieved

True_Retrieved=(rand()%(.15-True_Link))+True_Link;

am I doing this correct or am I WAYYYY off? 我是在正确执行此操作还是WAYYYY关闭? thanks 谢谢

rand() is a very simple Random Number Generator. rand()是一个非常简单的随机数生成器。 The Boost libraries include Boost.Random. Boost库包括Boost.Random。 In addition to random number generators, Boost.Random provides a set of classes to generate specific distirbutions. 除随机数生成器外,Boost.Random还提供了一组用于生成特定分布的类。 It sounds like you would want a distribution that's random between 1% and 10%, ie 0.01 and 0.1 . 听起来您会希望分布在1%和10%之间的随机分布,即0.010.1 That's done with boost::random::uniform_real(0.01, 0.1) . 这是通过boost::random::uniform_real(0.01, 0.1)

也许最好使用像Mersenne Twister这样的高级随机生成器。

rand() produces values between 0.0 and 1.0 inclusive, you have to scale that output to the interval you want. rand()产生介于0.0和1.0之间的值(包括0.1和1.0),您必须将该输出缩放到所需的时间间隔。 To get a value fact1 between 0.01 and 0.1 (1%-10%) you'd do: 要获得介于0.01和0.1(1%-10%)之间的值fact1 ,您可以执行以下操作:

perc1 = (rand()/RAND_MAX)*9.0+1.0; //percentage 1-10 on the 0-100 scale
fact1 = perc1/100.0;  //factor 0.01 - 0.1 on the 0-1 scale

to get a value between perc1 and 0.15 you'd do: 要获得介于perc1和0.15之间的值,您可以执行以下操作:

percrange = (15.0 - perc1);
perc2 = (rand()/RAND_MAX)*percrange + perc1;
fact2 = perc2/100.0;

so your values become: 因此您的价值观变成:

True_Links = fact1*Links_Retrieved;
True_Retrieved = fact2*Links_Retrieved;

This is sort-of-pseudocode. 这是伪代码排序。 You should make sure parc1, perc2, fact1, fact2 and percrange are floating point values, and the final multiplications are done in floating point and rounded to integer numbers. 您应该确保parc1, perc2, fact1, fact2percrange是浮点值,最后的乘法是在浮点数中进行的,并四舍五入为整数。

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

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