libft
Loading...
Searching...
No Matches
Files | Functions
Utilities Functions

Utility functions for general-purpose operations. More...

Files

file  ft_utils.c
 General-purpose utility functions.
 
file  ft_utils.h
 Utility functions for general-purpose operations.
 

Functions

int ft_rand (int min, int max)
 Returns a random integer in the range [min, max] using xorshift32.
 
void ft_swap (void *a, void *b, size_t size)
 Swaps the contents of two memory blocks.
 
static unsigned int xorshift32 (unsigned int *state)
 Performs a 32-bit xorshift operation to generate a pseudo-random number.
 

Detailed Description

Utility functions for general-purpose operations.

This group contains utility functions like ft_swap and ft_rand, which perform general operations that are often needed in various situations. These functions include swapping values and generating random numbers.

Function Documentation

◆ ft_rand()

int ft_rand ( int  min,
int  max 
)

#include <include/ft_utils.h>

Returns a random integer in the range [min, max] using xorshift32.

This function uses the xorshift32 algorithm to generate a random number between the specified minimum and maximum values, inclusive. The state is initialized once using the system time and is then used to produce random values.

Parameters
minThe minimum value of the range (inclusive).
maxThe maximum value of the range (inclusive).
Returns
A random integer between min and max, inclusive.
See also
xorshift32

◆ ft_swap()

void ft_swap ( void *  a,
void *  b,
size_t  size 
)

#include <include/ft_utils.h>

Swaps the contents of two memory blocks.

This function swaps the contents of two memory locations a and b of the specified size. The swap is performed using a temporary buffer to prevent data loss. It is essential to ensure that both pointers are non-NULL, and that the size is greater than zero for the function to work.

The function internally uses ft_memcpy to copy the contents between the two locations. After the swap, the temporary buffer is freed to avoid memory leaks.

Parameters
aA pointer to the first memory block.
bA pointer to the second memory block.
sizeThe size of the memory blocks being swapped.
Note
If either a, b, or size is invalid (NULL or zero), the function does nothing.
See also
ft_memcpy

◆ xorshift32()

static unsigned int xorshift32 ( unsigned int *  state)
static

#include <srcs/utilities/ft_utils.c>

Performs a 32-bit xorshift operation to generate a pseudo-random number.

This function implements the xorshift32 algorithm, a simple random number generator that uses bitwise operations to produce pseudo-random numbers. It modifies the provided state and returns the resulting random number.

Parameters
stateA pointer to the state value that is modified by the algorithm.
Returns
The generated random number based on the current state.
See also
ft_rand