don't give me random libary and such |
The
cstdlib is no less a "library" than what the
random is. They are both part of the same standard library, but the use of
rand() is
deprecated.
Deprecated features can be omitted from the next version of the standard library. In other words, when that happens, all the programs that have been using rand() and have to be compiled either cannot use the future library or have to be changed the other style.
Do you want to learn two different styles? Would it not be easier for you to simply learn the use of std::uniform_real_distribution?
just a simple rand command |
That would be a disservice to you. The very first link of TarikNeaj's first post describes the rand(). It tells what range of values the rand() does return. What is left to do, is to
scale that range into your desired range.
For example, say that you get range [0,3). Four different values. 0, 1, 2, 3.
0.0/4, 1.0/4, 2.0/4, 3.0/4, i.e. 0.0, 0.25, 0.5, 0.75 are in range [0,1).
The only tricky thing is that integer division does not work for this task. You have to force floating point division. That requires
static_cast
. See
http://en.wikipedia.org/wiki/Static_cast