大象传媒

Random number generation

Sometimes a programmer needs the to generate a number or letter within a specific range. This requires the program to create a random number. Programming games that involve chance, such as dice, require random numbers as do many types of program that need to be unpredictable.

Wheel with numbers in a random order
dice 鈫 RANDOM_INT(1,6)
OUTPUT 鈥測ou rolled a 鈥 + INT_TO_STRING(dice) + 鈥!鈥

Random numbers are not just used for rolling dice and games of chance. When sending across a , a wireless waits for a random amount of time before transmitting the data to avoid collisions. Random numbers are often used to generate secure passwords that are difficult to crack.