BEEP-8 Helper Lib 1.0.0
Loading...
Searching...
No Matches
mt.h File Reference

Header file for the Mersenne Twister random number generator. More...

#include <stdint.h>

Go to the source code of this file.

Functions

void init_genrand (unsigned long s)
 Initializes the state of the Mersenne Twister with a seed.
 
void init_by_array (unsigned long init_key[], int key_length)
 Initializes the state of the Mersenne Twister with an array.
 
uint32_t genrand_int32 (void)
 Generates a random number on [0, 0xffffffff] interval.
 
int32_t genrand_int31 (void)
 Generates a random number on [0, 0x7fffffff] interval.
 
int32_t genrand_min_max (int min_, int max_)
 Generates a random number in the specified range [min_, max_].
 

Detailed Description

Header file for the Mersenne Twister random number generator.

This header file declares functions for initializing and generating random numbers using the Mersenne Twister algorithm (MT19937). The implementation is based on the original code by Takuji Nishimura and Makoto Matsumoto.

The Mersenne Twister is a pseudorandom number generator that is widely used due to its high period (2^19937-1) and good statistical properties.

This implementation is not thread-safe. For multithreaded applications, ensure that each thread has its own instance of the Mersenne Twister state.

Usage

Before generating random numbers, initialize the state using either init_genrand or init_by_array.

Example

#include <mt.h>
#include <stdio.h>
int main() {
init_genrand(1234); // Initialize with a seed
for (int i = 0; i < 10; ++i) {
printf("%u\n", genrand_int32()); // Generate random numbers
}
return 0;
}
Header file for the Mersenne Twister random number generator.
uint32_t genrand_int32(void)
Generates a random number on [0, 0xffffffff] interval.
Definition mt.c:110
void init_genrand(unsigned long s)
Initializes the state of the Mersenne Twister with a seed.
Definition mt.c:65
Version
1.0
Date
2024

Function Documentation

◆ genrand_int31()

int32_t genrand_int31 ( void )
extern

Generates a random number on [0, 0x7fffffff] interval.

This function generates a 31-bit random number in the range [0, 0x7fffffff].

Returns
A 31-bit signed random number.

◆ genrand_int32()

uint32_t genrand_int32 ( void )
extern

Generates a random number on [0, 0xffffffff] interval.

This function generates a 32-bit random number in the range [0, 0xffffffff].

Returns
A 32-bit unsigned random number.
Examples
/Users/kzm/git/beep8-sdk/sdk/b8helper/include/sublibc.h.

◆ genrand_min_max()

int32_t genrand_min_max ( int min_,
int max_ )
extern

Generates a random number in the specified range [min_, max_].

This function generates a random number in the inclusive range [min_, max_].

Parameters
min_The minimum value of the range.
max_The maximum value of the range.
Returns
A random number in the specified range.

◆ init_by_array()

void init_by_array ( unsigned long init_key[],
int key_length )
extern

Initializes the state of the Mersenne Twister with an array.

This function initializes the internal state array using an array of seeds.

Parameters
init_keyThe array of seed values.
key_lengthThe length of the seed array.

◆ init_genrand()

void init_genrand ( unsigned long s)
extern

Initializes the state of the Mersenne Twister with a seed.

This function initializes the internal state array with a seed.

Parameters
sThe seed value.