English 中文(简体)
Rand_Max* (max-min)+min <?
原标题:Rand_Max*(max-min)+min << what is that?
generate(vec.begin(), vec.end(), [=](){return static_cast<T>(static_cast<double>(ran())
/RAND_MAX*(max-min)+min); });

问题: RAND_max-min;

因此,我知道算法、手提法表述,并在此内容中包含条款。 我的问题是完全如此。 以上大胆的案文意味着什么。 我要说的是,我知道自己是随机产生价值进程的一部分。 但是,没有确切知道损失发生在哪里。 因此,有人会pl倒这种微小的法典。

最佳回答

static_cast<double>ran()/RAND_ Fair*(max-min)+min”;

I m assuming you mistyped rand(), which returns a pseudorandom integer from 0 to RAND_MAX. Let s rewrite that in a way that clarifies the precedence a bit:

(T)(((双重) rand()/ RAND_ Fair) *(最大)+ min

因此:

  1. rand(): take a random integer between 0 and RAND_MAX
  2. (double) / RAND_MAX: divide as double by RAND_MAX, yielding a uniformly distributed double between 0 and 1:
  3. * (max-min): multiply by the range (max-min), yielding a double from 0 to (max-min)
  4. +min: add the minimum to yield a random double between min and max
  5. static_cast<T>: cast it back to the original type

结果是在<代码>min和max之间统一分发的T类随机编号。

问题回答

The expression static_cast<double>rand()/RAND_MAX creates a number between 0.0 and 1.0
When you multiply it by (max-min), you change the range, and when you add to min you shift the range. So, after that expression you have a random number (double) that ranges from min to max.

它的随机功能限于下游(因为地块可以归还零),并限于最大程度,因为即使它归还了100%的顶峰,并且添加到顶点上,你也处于顶峰。

You ll need to look at the entire expression: static_cast<double>(rand()) /RAND_MAX*(max-min)+min). Which with explicit grouping looks like: (static_cast<double>(rand()) /RAND_MAX)*(max-min)+min).
The first group returns a random value between 0 & 1 since rand() returns a value in the range 0 to RAND_MAX. The second group translates the 0 to 1 range to a min to max range.

ran( ran)功能在min和max之间产生随机值?

有时,我们需要的是A和B之间的随机价值(性别和最大)。 因此,我们可以为此调整结果。

a is double, so we use a static_cast! 
a = rand()      ; 0     <=   a   <= RAND_MAX
a = a/RAND_MAX  ; 0     <=   a   <= 1
a = B * a       ; 0     <=   a   <= B
a = min +a      ; 0+min <=   a   <= B+min

a) 最大,

B+min = max
B = max-min

其他手法

a = rand()/RAND_MAX*(max-min) + min

最小和最大之间随机数量。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签