Randomize Function In Dev C++

-->

C library function - rand - The C library function int rand(void) returns a pseudo-random number in the range of 0 to RANDMAX.

Defines facilities for random number generation, allowing creation of uniformly distributed random numbers.

Requirements

  1. Nov 20, 2016 Let us learn how to find and print Random Number in C programming language. This C program makes use of the rand method to print the random numbers. However, the. However, the srand method can also be used to find the random numbers. What is rand method? The C rand function generates a pseudo-random number between 0 and a number defined.
  2. These are not standard C or C functions. In standard C and C there are srand and rand which are part of the header. These functions are probably part of Turbo C to improve compatibility with Turbo Pascal, where randomize and random are the default random number generator.

Header: <random>

Namespace: std

Randomize Function In Dev C Pdf

Note

The <random> library uses the `#include <initializer_list>' statement.

Summary

A random number generator is an object that produces a sequence of pseudo-random values. A generator that produces values that are uniformly distributed in a specified range is a Uniform Random Number Generator (URNG). A class template designed to function as a URNG is referred to as an engine if that class has certain common traits, which are discussed later in this article. A URNG can be—and usually is—combined with a distribution by passing the URNG as an argument to the distribution's operator() to produce values that are distributed in a manner that is defined by the distribution.

These links jump to the major sections of this article:

Quick Tips

Here are some tips to keep in mind when using <random>:

Randomize function in dev c 5
  • For most purposes, URNGs produce raw bits that must be shaped by distributions. (A notable exception to this is std::shuffle() because it uses a URNG directly.)

  • A single instantiation of a URNG or distribution cannot safely be called concurrently because running a URNG or distribution is a modifying operation. For more information, see Thread Safety in the C++ Standard Library.

  • Predefined typedefs of several engines are provided; this is the preferred way to create a URNG if an engine is being used.

  • The most useful pairing for most applications is the mt19937 engine with uniform_int_distribution, as shown in the code example later in this article.

There are many options to choose from in the <random> header, and any of them is preferable to the outdated C Runtime function rand(). For information about what's wrong with rand() and how <random> addresses these shortcomings, see this video.

Examples

The following code example shows how to generate some random numbers in this case five of them using a generator created with non-deterministic seed.

While these are high quality random numbers and different every time this program is run, they are not necessarily in a useful range. To control the range, use a uniform distribution as shown in the following code:

The next code example shows a more realistic set of use cases with uniformly distributed random number generators shuffling the contents of a vector and an array.

This code demonstrates two different randomizations—randomize a vector of integers and shuffle an array of indexed data—with a test template function. The first call to the test function uses the crypto-secure, non-deterministic, not-seedable, non-repeatable URNG random_device. The second test run uses mersenne_twister_engine as URNG, with a deterministic 32-bit constant seed, which means the results are repeatable. The third test run seeds mersenne_twister_engine with a 32-bit non-deterministic result from random_device. The fourth test run expands on this by using a seed sequence filled with random_device results, which effectively gives more than 32-bit non-deterministic randomness (but still not crypto-secure). For more information, read on.

Categorized Listing

Uniform Random Number Generators

URNGs are often described in terms of these properties:

  1. Period length: How many iterations it takes to repeat the sequence of numbers generated. The longer the better.

  2. Performance: How quickly numbers can be generated and how much memory it takes. The smaller the better.

  3. Quality: How close to true random numbers the generated sequence is. This is often called 'randomness'.

The following sections list the uniform random number generators (URNGs) provided in the <random> header.

Non-Deterministic Generator

random_device ClassGenerates a non-deterministic, cryptographically secure random sequence by using an external device. Usually used to seed an engine. Low performance, very high quality. For more information, see Remarks.

Engine Typedefs with Predefined Parameters

For instantiating engines and engine adaptors. For more information, see Engines and Distributions.

  • default_random_engine The default engine.

  • knuth_b Knuth engine.

  • minstd_rand0 1988 minimal standard engine (Lewis, Goodman, and Miller, 1969).

  • minstd_rand Updated minimal standard engine minstd_rand0 (Park, Miller, and Stockmeyer, 1993).

  • mt19937 32-bit Mersenne twister engine (Matsumoto and Nishimura, 1998).

  • mt19937_64 64-bit Mersenne twister engine (Matsumoto and Nishimura, 2000).

  • ranlux24 24-bit RANLUX engine (Martin Lüscher and Fred James, 1994).

  • ranlux24_base Used as a base for ranlux24.

  • ranlux48 48-bit RANLUX engine (Martin Lüscher and Fred James, 1994).

  • ranlux48_base Used as a base for ranlux48.

Engine Templates

Engine templates are used as standalone URNGs or as base engines passed to engine adaptors. Usually these are instantiated with a predefined engine typedef and passed to a distribution. For more information, see the Engines and Distributions section.

linear_congruential_engine ClassGenerates a random sequence by using the linear congruential algorithm. Most simplistic and lowest quality.
mersenne_twister_engine ClassGenerates a random sequence by using the Mersenne twister algorithm. Most complex, and is highest quality except for the random_device class. Very fast performance.
subtract_with_carry_engine ClassGenerates a random sequence by using the subtract-with-carry algorithm. An improvement on linear_congruential_engine, but much lower quality and performance than mersenne_twister_engine.
Randomize Function In Dev C++

Engine Adaptor Templates

Engine adaptors are templates that adapt other (base) engines. Usually these are instantiated with a predefined engine typedef and passed to a distribution. For more information, see the Engines and Distributions section.

discard_block_engine ClassGenerates a random sequence by discarding values returned by its base engine.
independent_bits_engine ClassGenerates a random sequence with a specified number of bits by repacking bits from the values returned by its base engine.
shuffle_order_engine ClassGenerates a random sequence by reordering the values returned from its base engine.

[Engine Templates]

Random Number Distributions

The following sections list the distributions provided in the <random> header. Distributions are a post-processing mechanism, usually using URNG output as input and distributing the output by a defined statistical probability density function. For more information, see the Engines and Distributions section.

Uniform Distributions

uniform_int_distribution ClassProduces a uniform integer value distribution across a range in the closed interval [a, b] (inclusive-inclusive).
uniform_real_distribution ClassProduces a uniform real (floating-point) value distribution across a range in the half-open interval [a, b) (inclusive-exclusive).
generate_canonicalProduces an even distribution of real (floating point) values of a given precision across [0, 1) (inclusive-exclusive).

[Random Number Distributions]

Bernoulli Distributions

bernoulli_distribution ClassProduces a Bernoulli distribution of bool values.
binomial_distribution ClassProduces a binomial distribution of integer values.
geometric_distribution ClassProduces a geometric distribution of integer values.
negative_binomial_distribution ClassProduces a negative binomial distribution of integer values.

[Random Number Distributions]

Normal Distributions

cauchy_distribution ClassProduces a Cauchy distribution of real (floating point) values.
chi_squared_distribution ClassProduces a chi-squared distribution of real (floating point) values.
fisher_f_distribution ClassProduces an F-distribution (also known as Snedecor's F distribution or the Fisher-Snedecor distribution) of real (floating point) values.
lognormal_distribution ClassProduces a log-normal distribution of real (floating point) values.
normal_distribution ClassProduces a normal (Gaussian) distribution of real (floating point) values.
student_t_distribution ClassProduces a Student's t-distribution of real (floating point) values.

[Random Number Distributions]

Poisson Distributions

exponential_distribution ClassProduces an exponential distribution of real (floating point) values.
extreme_value_distribution ClassProduces an extreme value distribution of real (floating point) values.
gamma_distribution ClassProduces a gamma distribution of real (floating point) values.
poisson_distribution ClassProduces a Poisson distribution of integer values.
weibull_distribution ClassProduces a Weibull distribution of real (floating point) values.

[Random Number Distributions]

Sampling Distributions

discrete_distribution ClassProduces a discrete integer distribution.
piecewise_constant_distribution ClassProduces a piecewise constant distribution of real (floating point) values.
piecewise_linear_distribution ClassProduces a piecewise linear distribution of real (floating point) values.

[Random Number Distributions]

Utility Functions

This section lists the general utility functions provided in the <random> header.

seed_seq ClassGenerates a non-biased scrambled seed sequence. Used to avoid replication of random variate streams. Useful when many URNGs are instantiated from engines.

Operators

This section lists the operators provided in the <random> header.

operatorTests whether the URNG on the left side of the operator is equal to the engine on the right side.
operator!=Tests whether the URNG on the left side of the operator is not equal to the engine on the right side.
operator<<Writes state information to a stream.
operator>>Extracts state information from a stream.

Engines and Distributions

Refer to the following sections for information about each of these class template categories defined in <random>. Both of these class template categories take a type as an argument and use shared template parameter names to describe the properties of the type that are permitted as an actual argument type, as follows:

  • IntType indicates a short, int, long, long long, unsigned short, unsigned int, unsigned long, or unsigned long long.

  • UIntType indicates unsigned short, unsigned int, unsigned long, or unsigned long long.

  • RealType indicates a float, double, or long double.

Engines

Engine Templates and Engine Adaptor Templates are templates whose parameters customize the generator created.

An engine is a class or class template whose instances (generators) act as a source of random numbers uniformly distributed between a minimum and maximum value. An engine adaptor delivers a sequence of values that have different randomness properties by taking values produced by some other random number engine and applying an algorithm of some kind to those values.

Every engine and engine adaptor has the following members:

  • typedefnumeric-typeresult_type is the type that is returned by the generator's operator(). The numeric-type is passed as a template parameter on instantiation.

  • result_type operator() returns values that are uniformly distributed between min() and max().

  • result_type min() returns the minimum value that is returned by the generator's operator(). Engine adaptors use the base engine's min() result.

  • result_type max() returns the maximum value that is returned by the generator's operator(). When result_type is an integral (integer-valued) type, max() is the maximum value that can actually be returned (inclusive); when result_type is a floating-point (real-valued) type, max() is the smallest value greater than all values that can be returned (non-inclusive). Engine adaptors use the base engine's max() result.

  • void seed(result_type s) seeds the generator with seed value s. For engines, the signature is void seed(result_type s = default_seed) for default parameter support (engine adaptors define a separate void seed(), see next subsection).

  • template <class Seq> void seed(Seq& q) seeds the generator by using a seed_seqSeq.

  • An explicit constructor with argument result_type x that creates a generator seeded as if by calling seed(x).

  • An explicit constructor with argument seed_seq& seq that creates a generator seeded as if by calling seed(seq).

  • void discard(unsigned long long count) effectively calls operator()count times and discards each value.

Engine adaptors additionally support these members (Engine is the first template parameter of an engine adaptor, designating the base engine's type):

  • A default constructor to initialize the generator as if from the base engine's default constructor.

  • An explicit constructor with argument const Engine& eng. This is to support copy construction using the base engine.

  • An explicit constructor with argument Engine&& eng. This is to support move construction using the base engine.

  • void seed() that initializes the generator with the base engine's default seed value.

  • const Engine& base() property function that returns the base engine that was used to construct the generator.

Every engine maintains a state that determines the sequence of values that will be generated by subsequent calls to operator(). The states of two generators instantiated from engines of the same type can be compared by using operator and operator!=. If the two states compare as equal, they will generate the same sequence of values. The state of an object can be saved to a stream as a sequence of 32-bit unsigned values by using the operator<< of the generator. The state is not changed by saving it. A saved state can be read into generator instantiated from an engine of the same type by using operator>>.

Distributions

A Random Number Distributions is a class or class template whose instances transform a stream of uniformly distributed random numbers obtained from an engine into a stream of random numbers that have a particular distribution. Every distribution has the following members:

  • typedefnumeric-typeresult_type is the type that is returned by the distribution's operator(). The numeric-type is passed as a template parameter on instantiation.

  • template <class URNG> result_type operator()(URNG& gen) returns values that are distributed according to the distribution's definition, by using gen as a source of uniformly distributed random values and the stored parameters of the distribution.

  • template <class URNG> result_type operator()(URNG& gen, param_type p) returns values distributed in accordance with the distribution's definition, using gen as a source of uniformly distributed random values and the parameters structure p.

  • typedefunspecified-typeparam_type is the package of parameters optionally passed to operator() and is used in place of the stored parameters to generate its return value.

  • A const param& constructor initializes the stored parameters from its argument.

  • param_type param() const gets the stored parameters.

  • void param(const param_type&) sets the stored parameters from its argument.

  • result_type min() returns the minimum value that is returned by the distribution's operator().

  • result_type max() returns the maximum value that is returned by the distribution's operator(). When result_type is an integral (integer-valued) type, max() is the maximum value that can actually be returned (inclusive); when result_type is a floating-point (real-valued) type, max() is the smallest value greater than all values that can be returned (non-inclusive).

  • void reset() discards any cached values, so that the result of the next call to operator() does not depend on any values obtained from the engine before the call.

A parameter structure is an object that stores all of the parameters needed for a distribution. It contains:

  • typedefdistribution-typedistribution_type, which is the type of its distribution.

  • One or more constructors that take the same parameter lists as the distribution constructors take.

  • The same parameter-access functions as the distribution.

  • Equality and inequality comparison operators.

For more information, see the reference subtopics below this one, linked previously in this article.

There are two highly useful URNGs in Visual Studio—mt19937 and random_device—as shown in this comparison table:

URNGFastCrypto-secureSeedableDeterministic
mt19937YesNoYesYes*
random_deviceNoYesNoNo

* When provided with a known seed.

Although the ISO C++ Standard does not require random_device to be cryptographically secure, in Visual Studio it is implemented to be cryptographically secure. (The term 'cryptographically secure' does not imply guarantees, but refers to a minimum level of entropy—and therefore, the level of predictability—a given randomization algorithm provides. For more information, see the Wikipedia article Cryptographically secure pseudorandom number generator.) Because the ISO C++ Standard does not require this, other platforms may implement random_device as a simple pseudo-random number generator (not cryptographically secure) and may only be suitable as a seed source for another generator. Check the documentation for those platforms when using random_device in cross-platform code.

By definition, random_device results are not reproducible, and a side-effect is that it may run significantly slower than other URNGs. Most applications that are not required to be cryptographically secure use mt19937 or a similar engine, although you may want to seed it with a call to random_device, as shown in the code example.

See also

  • The C Standard Library
  • C Standard Library Resources
  • C Programming Resources
  • Selected Reading

Description

The C library function int rand(void) returns a pseudo-random number in the range of 0 to RAND_MAX.

RAND_MAX is a constant whose default value may vary between implementations but it is granted to be at least 32767.

Declaration

Following is the declaration for rand() function.

Randomize Function In Dev C 2017

Parameters

  • NA

Return Value

This function returns an integer value between 0 and RAND_MAX.

Example

The following example shows the usage of rand() function.

Let us compile and run the above program that will produce the following result −

Randomize Function In Dev C Online

stdlib_h.htm