stdlib.h
stdlib stands for standard library. It is a collection of general purpose functions that are not really related to any specific concept. In our discussion we will focus on a few functions that may be more useful for you.
Conversion
To convert a string to some other type there are a number of functions that you can use from stdlib. The name of the functions indicate how they are suppose to be used. The a means ascii, to is exactly what it means... and the last letter indicates what it converts to. thus atoi converts to an int. note that the f is refering to floating point not float, thus the atof() returns a double.
| Function | Purpose | Expected Behavior | Example |
|---|---|---|---|
atoi() | String to int | Converts string to integer | int x = atoi("42"); |
atol() | String to long | Converts string to long | long x = atol("42"); |
atof() | String to double | Converts string to floating-point | double x = atof("3.14"); |
random number generators
There isn't any such thing as true random number generation. Random number generators are also called pseudo random number generators because they are not truly random. This section will explain how they work and how to use them.
This section will focus on the rand() function from stdlib.h library. Please note that there are other random number generators available for different purposes. If generating random numbers are important for your application, you should look for a library specifically built for this purpose. Writing random number generators to generate random numbers that meet correct statistical requirements is a task that requires expert knowledge.
rand()
The rand() function returns a number between 0 and RAND_MAX inclusive. the value of RAND_MAX is defined in stdlib.h and different compilers can have different RAND_MAX. It is guaranteed that RAND_MAX is at minimum 32767.
How Random Number Generators work.
Random number generators generate a sequence of numbers that seem random when relative to each other.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i;
for(i = 0 ;i<5;i++){
printf("%d\n", rand());
}
}
The above would generate 5 numbers that do not seem to have any relationship to each other...5 "random" numbers.
However, the numbers are not truly random. If you were to take this program and you ran it multiple times, you will notice that if you ran the program over and over, the same 5 numbers will always be generated.
This is because these numbers are just a series of calculations based on a starting seed value. Think of all random number generators as a sequence of values that look random when one value in the sequence is compared to the next. The seed is simply a way to indicate the starting point of that sequence. As we did not specifically indicate the seed for the above code, our random numbers all use the default seed. As that number is not changing, every run of our program uses exactly the same seed and thus we get the same sequence.
Seeding our Random Number Generator
To seed our random number generator in C, we use the srand() function:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i;
//seed the random number generator with the number 2
srand(2);
for(i = 0 ;i<5;i++){
printf("%d\n", rand());
}
}
If you change the number in the srand() function call you will get a different sequence of 5 random numbers.
However, as you can see if you specify a specific value for the seed like 2, your sequence of random numbers will still be the same from one run to the next. it might be different from the initial 5 when you didn't call srand() but its the same sequence of as any run with srand(2).
This is why the practice is to actually use time() function. This way, each time you run the program, you would likely be doing so at a different time, and therefore seed the random number generator with a different time and therefore getting a different sequence.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int i;
srand(time(NULL))
for(i = 0 ;i<5;i++){
printf("%d\n", rand());
}
}
Here are the things we should pay attention to when it comes to seeding our random number generators:
- You only seed ONCE in your program (there is a bit more to it than this.. but this is good enough for general purpose use of random number generators). Calling srand() multiple times would simply place you at a different point in the random number sequence so it is not necessary. It could in fact lead to unexpected results if you call it multiple times.
- If you have a bug in your program, you should change your seed to a constant value as opposed to time(NULL). This way you can more easily debug your program because the random numbers will always be the same sequence. Your bug won't "move" because of the random number generated.