bitlib_c  0.2.4
Functions
random.h File Reference
#include <stdbool.h>

Go to the source code of this file.

Functions

void rand_seed (int seed)
 
void rand_seed_rand ()
 
bool rand_weighted_bool (double percent)
 
bool rand_bool ()
 
int rand_int ()
 
double rand_double ()
 
double rand_double_range (double min, double max)
 
int rand_int_range (int min, int max)
 
double rand_power (double min, double max, double power)
 
double rand_gauss (double min, double max, int gauss)
 
void rand_string (char *str, int length)
 
void rand_string_lower (char *str, int length)
 
void rand_string_upper (char *str, int length)
 
void rand_string_alpha (char *str, int length)
 

Function Documentation

◆ rand_bool()

bool rand_bool ( )

Returns a random boolean.

Returns
double

Examples

bool b = rand_bool(); // b has a 50% chance of being true.

◆ rand_double()

double rand_double ( )

Returns a random double from 0 to 1.

Returns
double

Examples

double n = rand_double();

◆ rand_double_range()

double rand_double_range ( double  min,
double  max 
)

Returns a double between a min and max value.

Parameters
doublemin The minimum value of the range.
doublemax The maximum value of the range.
Returns
double

Examples

double n = rand_double_range(50, 100);

◆ rand_gauss()

double rand_gauss ( double  min,
double  max,
int  gauss 
)

Returns a double within a range, weighted with a bell curve towards the center of the range.

The gauss parameter affects the strength of the bell curve. The higher gauss is set, the more likely the return values will tend towards the center of the range. A gauss of 1 makes this function work exactly like rand_double_range.

Parameters
doublemin The minimum value of the range.
doublemax The maximum value of the range.
doublegauss The strength of the bell curve.
Returns
double

Examples

double n = rand_gauss(0, 100, 2); // n will be more somewhat more likely to be closer to 50 than a random range.
double m = rand_gauss(0, 100, 10); // m will be very much more likely to be closer to 50.

◆ rand_int()

int rand_int ( )

Returns a random integer from 0 to MAX_RAND.

Returns
int

Examples

int n = rand_int();

◆ rand_int_range()

int rand_int_range ( int  min,
int  max 
)

Returns a int between a min and max value.

Parameters
intmin The minimum value of the range.
intmax The maximum value of the range.
Returns
int

Examples

int n = rand_int_range(50, 100);

◆ rand_power()

double rand_power ( double  min,
double  max,
double  power 
)

Returns a double within a range, weighted exponentially towards one end of the range.

The power parameter affects where the return value will end up. A power value of one makes this function work exactly like rand_double_range. As the power value decreases towards zero, the return values will tend towards maximum, and as power increases above 1, the return values will tend towards minimum.

Parameters
doublemin The minimum value of the range.
doublemax The maximum value of the range.
doublepower The power of the exponent that affects the result.
Returns
double

Examples

double n = rand_power(0, 100, 2); // n will be more likely to be closer to 0.
double m = rand_power(0, 100, 0.2); // m will be more likely to be closer to 100.

◆ rand_seed()

void rand_seed ( int  seed)

Sets the seed used for generating random values.

Calling the same series of rand functions after setting the seed to the same value will result in the same series of random results.

Parameters
intseed The seed value.

Examples

rand_seed(0);
double a = rand_double();
int b = rand_int();

rand_seed(0);
double c = rand_double(); // c will equal a
int d = rand_int(); // d will equal b

◆ rand_seed_rand()

void rand_seed_rand ( )

Sets the seed used for generating random values.

This actually sets the seed using a couple of different time functions designed to give a fairly decent random seed.

Examples

rand_seed_rand();
int b = rand_int(); // who knows what b will equal?

◆ rand_string()

void rand_string ( char *  str,
int  length 
)

Creates a random string which may contain upper and lower case characters, digits and other ascii symbols.

The char array that is passed in should have a length of at least length + 1 to account for the null terminator.

Parameters
char*str A char array that will receive the random string.
intlength The length of the string returned.
Returns
char*

Examples

char str[11];
rand_string(str, 10);
printf("str: %s\n", str);

◆ rand_string_alpha()

void rand_string_alpha ( char *  str,
int  length 
)

Creates a random string of upper and lower case letters.

The char array that is passed in should have a length of at least length + 1 to account for the null terminator.

Parameters
char*str A char array that will receive the random string.
intlength The length of the string returned.
Returns
char*

Examples

char str[11];
rand_string_alpha(str, 10);
printf("str: %s\n", str);

◆ rand_string_lower()

void rand_string_lower ( char *  str,
int  length 
)

Creates a random string of lower case letters. The char array that is passed in should have a length of at least length + 1 to account for the null terminator.

Parameters
char*str A char array that will receive the random string.
intlength The length of the string returned.
Returns
char*

Examples

char str[11];
rand_string_lower(str, 10);
printf("str: %s\n", str);

◆ rand_string_upper()

void rand_string_upper ( char *  str,
int  length 
)

Creates a random string of upper case letters.

The char array that is passed in should have a length of at least length + 1 to account for the null terminator.

Parameters
char*str A char array that will receive the random string.
intlength The length of the string returned.
Returns
char*

Examples

char str[11];
rand_string_upper(str, 10);
printf("str: %s\n", str);

◆ rand_weighted_bool()

bool rand_weighted_bool ( double  percent)

Returns a random boolean. Allows you to weight the odds of getting true or false.

Parameters
doublepercent The odds of getting true. 0 it 0%. 0.5 is 50%. 1.0 is 100%.
Returns
double

Examples

bool b = rand_weighted_bool(0.2); // b has a 20% chance of being true.