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 - C Tutorial
From the course: C Standard Library
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
Memory allocation1m 32s
-
Memory allocation example2m 29s
-
Random number generation1m 48s
-
Random number generation example2m 40s
-
String conversion2m 11s
-
String conversion example6m 29s
-
Sort and search1m 52s
-
Sort and search main4m 50s
-
Sort and search functions5m 6s
-
Challenge: Let's get random1m 38s
-
Solution: Let's get random5m 40s
-
-
-
-