Rate This Document
Findability
Accuracy
Completeness
Readability

Uniform Distribution Function

Generate a random number vector that conforms to uniform distribution.

Interface Definition

int vsRngUniform (const int method, VSLStreamStatePtr stream, const SizeType n, float *r, const float a, const float b);

int vdRngUniform (const int method, VSLStreamStatePtr stream, const SizeType n, double *r, const double a, const double b);

int viRngUniform(const int method, VSLStreamStatePtr stream, const SizeType n, int *r, const int a, const int b);

int viRngUniformBits32(const int method, VSLStreamStatePtr stream, const SizeType n, uint32_t *r);

int viRngUniformBits64(const int method, VSLStreamStatePtr stream, const SizeType n, uint64_t *r);

int viRngUniformBits(const int method, VSLStreamStatePtr stream, const SizeType n, unsigned int *r);

Probability Density Function

Parameters

Parameter

Type

Description

Input/Output

method

int

Random number generation method.

  • VSL_RNG_METHOD_UNIFORM_STD: standard method.
  • VSL_RNG_METHOD_UNIFORM_STD_ACCURATE: accurate method.

Input

stream

VSLStreamStatePtr type

Random number stream.

Input

n

SizeType

Length of the output vector.

Input

r

  • Single-precision floating-point pointer for vsRngUniform
  • Double-precision floating-point pointer for vdRngUniform
  • Integer pointer for viRngUniform
  • 32-bit unsigned integer pointer for viRngUniformBits32
  • 64-bit unsigned integer pointer for viRngUniformBits64
  • Unsigned integer pointer for viRngUniformBits

Pointer to the result.

Output

a

  • Single-precision floating-point type for vsRngUniform
  • Double-precision floating-point type for vdRngUniform
  • Integer for viRngUniform

Minimum value of the uniform distribution.

Input

b

  • Single-precision floating-point type for vsRngUniform
  • Double-precision floating-point type for vdRngUniform
  • Integer for viRngUniform

Maximum value of the uniform distribution.

Input

Dependencies

#include "krng.h"

Examples

#include <stdio.h>
#include <stdlib.h>
#include "krng.h"
int main()
{
   /* initialize stream with given BRNG type and seed */
    VSLStreamStatePtr stream;
    unsigned seed = 42;
    int errcode = vslNewStream(&stream, VSL_BRNG_MCG59, seed);
    if (errcode != VSL_STATUS_OK) {
        fprintf(stderr, "Failure in newstream\n");
        return 0;
    }

    SizeType n = 10;
    float *r = (float *)malloc(sizeof(float) * n);
    if (r == NULL) {
        fprintf(stderr, "Failure in malloc\n");
        return 0;
    }

    if (vsRngUniform(VSL_RNG_METHOD_UNIFORM_STD, stream, n, r, 0.0, 1.0)) {
        fprintf(stderr, "Failure in vsRngUniform\n");
        goto out;
    }

    /* deinitialize the stream */
    errcode = vslDeleteStream(&stream);
    if (errcode != VSL_STATUS_OK) {
        fprintf(stderr, "Failure in deleting stream\n");
        goto out;
    }

out:
    free(r);
    return 0;
}