# C++ rand() vs srand() ## ```rand()``` - Purpose: Generates a pseudo-random integer. - Return Type: int - Range: Between 0 and RAND_MAX (a constant defined in <cstdlib>, often 32767). - Usage ```c++ int random_number = rand(); // 0 to RAND_MAX ``` - Note - Without seeding, produces the same sequence every program run - Uses a deterministic algorithm (not truly random) ## ```srand()``` - Purpose: "Seeds" the random number generator - Parameter: Takes an unsigned integer seed value - Usage: ```c++ srand(123); // Set a fixed seed srand(time(0)); // Set a variable seed based on current time ``` - Effect: - Initializes the random number sequence - Different seeds produce different sequences - Same seed produces the same sequence ## Notes **What's seeding?** - Seeding is the process of initializing a pseudo-random number generator (PRNG) with a starting value, called a seed. - Why seeding matters - A pseudo-random number generator doesn't create truly random numbers — it creates a deterministic sequence that appears random, based on an initial seed value. - Same seed → Same sequence - Different seed → Different sequence - This is useful because: - You can reproduce results if you know the seed (great for debugging or simulations). - You can create different results each time by using a changing seed (like time(0)).