◆ generate_n() [2/2]

template<typename OutputIterator , typename Size , typename Generator >
OutputIterator thrust::generate_n ( OutputIterator  first,
Size  n,
Generator  gen 

generate_n assigns the result of invoking gen, a function object that takes no arguments, to each element in the range [first,first + n). The return value is first + n.

firstThe first element in the range of interest.
nThe size of the range of interest.
genA function argument, taking no parameters, used to generate values to assign to elements in the range [first,first + n).
Template Parameters
OutputIteratoris a model of Output Iterator.
Sizeis an integral type (either signed or unsigned).
Generatoris a model of Generator, and Generator's result_type is convertible to a type in OutputIterator's set of value_types.

The following code snippet demonstrates how to fill a host_vector with random numbers, using the standard C library function rand.

#include <stdlib.h>
thrust::host_vector<int> v(10);
thrust::generate_n(v.begin(), 10, rand);
// the elements of v are now pseudo-random numbers
See also