From the course: C Standard Library

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Random number generation example

Random number generation example - C Tutorial

From the course: C Standard Library

Start my 1-month free trial

Random number generation example

- [Instructor] So to illustrate these random, number-generation functions, let's write an application that will help us in raffle, where the numbers are from one to 10 thousand. So, we want to draw a number, a random number, and report it. So, let me start by using a variable in line eight that is an unsigned integer of 32 bits called winner. So, that's where we'll store the winner, and let's initialize the random number generator. That's srand for the random seed, and I will use the system time. This time function takes in an argument which can be zero for us to just take the current time. And now, let's write this program, so that we can draw several numbers. Maybe the raffle is for several prizes. So, let's write this in a while loop, and I will use a terminal for this. So, and we'll use get char, and as long as I don't receive the character x, I will keep on drawing numbers. And now we need to call the rand function. So, my winner variable will equal rand. Now, remember, rand is a…

Contents