Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codefactor pull request related to rand vs. rand_r #111

Open
craigsapp opened this issue Apr 22, 2024 · 0 comments
Open

Codefactor pull request related to rand vs. rand_r #111

craigsapp opened this issue Apr 22, 2024 · 0 comments

Comments

@craigsapp
Copy link
Owner

https://www.codefactor.io/repository/github/craigsapp/midifile/pull/107

says that rand_r is thread-safe compared to rand. But rand_r is potentially less portable. So maybe use C++ random functions instead.

Used in MidiMessage::makeTemperamentBad, for example.


Differences between rand() and rand_r():

rand() and rand_r() are both functions used to generate pseudo-random numbers in C programming, but they have some important differences that make them suitable for different situations:

Thread Safety:

rand(): This function is not thread-safe, which means if it is used in a multi-threaded application, you could encounter data races or inconsistent results when multiple threads call rand() at the same time.
rand_r(): This function is thread-safe. It requires a pointer to an unsigned int (often referred to as the seed) to be passed to it. Each thread can maintain its own seed, thus avoiding interference between threads, which makes rand_r() suitable for use in multi-threaded applications.

Seed Management:

rand(): It uses a hidden seed that is shared across all uses of rand() in the program. You can set this seed with srand(), but all subsequent calls to rand() will be based on this shared seed.
rand_r(): It allows each caller to manage its own seed. You pass a pointer to this seed each time you call rand_r(), giving you control over the randomness sequence, which can be different for each seed.

Usage:

rand(): It’s simpler to use when you don’t have concerns about thread safety or when all random number generation can safely use the same sequence of numbers.
rand_r(): It’s better when you need different sequences of random numbers in different parts of your program, or when your program is multi-threaded.

Portability:

rand(): It is standardized by the ANSI C and ISO C standards, making it widely available in C environments.
rand_r(): It is not part of the ISO C standard and is considered POSIX-specific. Thus, its availability can vary depending on the system, particularly on non-UNIX-like systems.

Because of these differences, choosing between rand() and rand_r() depends largely on your application's requirements regarding thread safety and how you need to manage the random number generation's seed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant