C Random Number Generation

Source: Internet
Author: User
Tags random seed

The rand () function, srand () function, and C Language/C ++ do not have the built-in random (int number) function to generate random numbers.

Let's get to know the program first.

# Include <stdio. h> # include <stdlib. h> # include <time. h> # define random (x) (rand () % x) main () {int length, I; int wait; int array [10]; // define an array srand (time (NULL); // make the random number generated each time different length = sizeof (array)/sizeof (array [0]); // assign a value to the array for (I = 0; I <10; I ++) array [I] = random (100); // output array for (I = 0; I <10; I ++) printf ("% d \ n", array [I]); printf ("% d", length); scanf ("% d ", & wait );}

The two functions related to generating random numbers in the C language are srand (), rand (), which are included in the header file stdlib. h.

Make the random numbers generated by each execution different

Void srand (unsigned seed );

The srand () function is used to set the random number seed when rand () generates a random number. The seed parameter must be an integer. Generally, the return value of time (0) can be used as seed. If the same value is set for each seed, the random values generated by rand () are the same each time.

Int rand ()

The srand function accepts an unsigned parameter and sets a Random Seed for rand.

Rand () returns a random value ranging from 0 to RAND_MAX. RAND_MAX is defined in stdlib. h and its value is 2147483647. Before calling this function to generate a random number, you must use srand () to set the random number seed. If no random number seed is set, rand () will automatically set the random number seed to 1.

Typical usage:

srand(time(NULL));for( i = 0; i < 10; i++ )   printf( "Random number #%d: %d\n", i, rand() );

In this way, because the seed set each time is different, the same random number will not be generated during multiple running times.

However, in the following code, the random numbers generated during multiple runs are exactly the same.

srand(100);for(int i = 0; i < 10; i++ )   printf( "Random number #%d: %d\n", i, rand() );
Program reference for generating random numbers

If you only need to generate a random number without setting a range, you only need to use rand (): rand () returns a random value ranging from 0 to RAND_MAX. RAND_MAX is defined in stdlib. h and its value is 2147483647. For example:

#include<stdio.h>#include<stdlib.h>void main(){for(int i=0;i<10;i+)printf("%d\n",rand());}

If you want to generate a random number in a certain range, you can define a random (int number) function in the macro definition, and then directly call the random () function in main:

#include<stdio.h>#include<stdlib.h>#define random(x) (rand()%x)void main(){for(int x=0;x<10;x++)printf("%d\n",random(100));}

However, the random numbers generated in the preceding two examples can only be one-time. If the output result is still the same as that of the first running. This is related to the srand () function. Srand () is used to set the random number seed when rand () generates a random number. Before calling the rand () function to generate a random number, you must use srand () to set the random number seed. If no random number seed is set () during the call, the random seed is set to 1. In the above two examples, because no random number seed is set, each random number seed is automatically set to the same value of 1, which leads to the same random value produced by rand.

Srand () function definition: void srand (unsigned int seed); generally, the return value of geypid () or time (0) can be used as seed. If you use time (0, header file # include <time. h>

#include<stdio.h>#include<stdlib.h>#include<time.h>#define random(x) (rand()%x)void main(){srand((int)time(0));for(int x=0;x<10;x++)printf("%d\n",random(100));}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.