views:

238

answers:

5

This is an excerpt of some c++ code, that i'll have to explain in detail in some days:

std::vector<int> vct(8, 5);

std::generate(vct.begin(), vct.end(), &rand);

std::copy(vct.rbegin(), vct.rend(),
    std::ostream_iterator<int>(std::cout, "\n"));

i think i understand everything about it, except that tiny mystical &rand. what exactly does it do? i mean obviously it produces some kind of pseudo-random-numbers, but they always remain the same. where does the &rand come from? what kind of expression is it? where do the values it produces come from? i'm a bit confused…

also, i could neither find any other occurrences of the word "rand" in the code, nor did i see any code that could have something to do with random numbers. that made me wonder, because my (very limited) experience in c++ showed, that only very few things simply work without having to be declared or included previously.

thanks for helping out!

+2  A: 

rand is defined in cstdlib. You're getting the same value because you're using the same "seed". That's by design.

If you want a different seed, call srand first, e.g., srand(time(NULL));.

fatcat1111
It's `rand` not `rnd`.
Ken Bloom
Ug, typos will be the death of me. Thanks Ken.
fatcat1111
Hm can't decide whether yours or Ken's answer is better, but the combination is just what i needed. thanks!
padde
+12  A: 

The & in &rand returns the address of the rand() function. You're passing a function pointer to generate() so generate() can call rand() to generate random numbers.

Ken Bloom
A: 

rand() is a function that is part of the standard C library. It's signature is:

int rand(void);

It's defined in stdlib.h. Most likely, the implementation of your std C++ library is including that header file when you are including or .

R Samuel Klatchko
A: 

&rand just means "the address of rand." rand is a function from the standard library which returns pseudo-random numbers. What you're doing is passing the address of this function to the algorithms. They then call this function during the course of their work.

As far as rand itself goes, any decent C++ documentation will tell you why it behaves the way you're observing... (and other answers here do as well)

Cogwheel - Matthew Orlando
+5  A: 

rand() is a function from the C standard library which generates psuedo-random numbers between 0 and the define RAND_MAX. If you use C++, include the header <cstdlib> to use it (instead of <stdlib.h>) srand() lets you set a seed. If you want a new seed every time you run your application, you can seed with a value from time().

The POSIX standard from 2001 has an example-implementation of rand() and srand(). I copy-pasted it from my man pages. The implementation used by your C standard library might differ from this one:

static unsigned long next = 1;

/* RAND_MAX assumed to be 32767 */
int myrand(void) {
    next = next * 1103515245 + 12345;
    return((unsigned)(next/65536) % 32768);
}

void mysrand(unsigned seed) {
    next = seed;
}

Also, the & takes the address from the function. Compilers are kind of ambiguous on this part. Some require the use of this operator when you want addresses from function pointers; others don't. generate is a function from the <algorithm> header from the C++ standard library. It takes functions and/or functor objects and uses it to generate values for the container whos iterators you specify.

Mads Elvheim