generate_n#

Overloads#

generate_n(exec, first, n, gen)#

template<typename DerivedPolicy, typename OutputIterator, typename Size, typename Generator>
OutputIterator thrust::generate_n(
const thrust::detail::execution_policy_base<DerivedPolicy> &exec,
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.

The algorithm’s execution is parallelized as determined by exec.

The following code snippet demonstrates how to fill a host_vector with random numbers, using the standard C library function rand using the thrust::host execution policy for parallelization:

#include <thrust/generate.h>
#include <thrust/host_vector.h>
#include <thrust/execution_policy.h>
#include <cstdlib>
...
thrust::host_vector<int> v(10);
srand(13);
thrust::generate_n(thrust::host, v.begin(), 10, rand);

// the elements of v are now pseudo-random numbers

See also

generate

Parameters:
  • exec – The execution policy to use for parallelization.

  • first – The first element in the range of interest.

  • n – The size of the range of interest.

  • gen – A function argument, taking no parameters, used to generate values to assign to elements in the range [first,first + n).

Template Parameters:
  • DerivedPolicy – The name of the derived execution policy.

  • OutputIterator – is a model of Output Iterator.

  • Size – is an integral type (either signed or unsigned).

  • Generator – A nullary function with a return type convertible to ForwardIterator's value_type.

generate_n(first, n, gen)#

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.

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

#include <thrust/generate.h>
#include <thrust/host_vector.h>
#include <stdlib.h>
...
thrust::host_vector<int> v(10);
srand(13);
thrust::generate_n(v.begin(), 10, rand);

// the elements of v are now pseudo-random numbers

See also

generate

Parameters:
  • first – The first element in the range of interest.

  • n – The size of the range of interest.

  • gen – A function argument, taking no parameters, used to generate values to assign to elements in the range [first,first + n).

Template Parameters:
  • OutputIterator – is a model of Output Iterator.

  • Size – is an integral type (either signed or unsigned).

  • Generator – A nullary function with a return type convertible to ForwardIterator's value_type.