stdrand.h
is a simple C library that provides functions for generating random integers and making random choices from arrays of characters or integers.
Generates a random integer within the specified range [min, max]
(inclusive).
-
Parameters:
min
: The minimum value of the range.max
: The maximum value of the range.
-
Returns:
- A random integer within the specified range.
- If
min
is greater thanmax
, the function returns-1
.
Selects a random character from the provided character array.
-
Parameters:
arr
: A pointer to a null-terminated character array.
-
Returns:
- A randomly chosen character from the array.
Selects a random integer from the provided integer array.
-
Parameters:
arr
: A pointer to an array of integers.s
: The size of the integer array.
-
Returns:
- A randomly chosen integer from the array.
Generates a random character between two given characters a
and b
.
-
Parameters:
a
: The lower bound character.b
: The upper bound character.
-
Returns:
- A random character between
a
andb
.
- A random character between
- Include the
stdrand.h
header in your C source code. - Call the appropriate functions based on your requirements.
#include "stdrand.h"
#include <stdio.h>
int main() {
long randomNum = rand_int(10, 20);
printf("Random number between 10 and 20: %ld\n", randomNum);
char choices[] = "ABCDEF";
char randomChar = rand_choice_char(choices);
printf("Random character: %c\n", randomChar);
int numArray[] = {100, 200, 300, 400, 500};
long randomIntArray = rand_choice_int(numArray, sizeof(numArray) / sizeof(int));
printf("Random integer from array: %ld\n", randomIntArray);
char randomGeneratedChar = rand_char('A', 'Z');
printf("Random generated character between A and Z: %c\n", randomGeneratedChar);
return 0;
}
This library is released under the MIT License.