Random number between 0-1 C + +

Source: Internet
Author: User
Tags random seed



First post code (6 digits after decimal point) vc6.0 test


#include "stdafx.h" #include <time.h> #include <stdlib.h> int main(int argc, char* argv[])
{ double d = 0.0; int nCount = 0;

    srand((int)time(0)); for(int x=0;x<1000;x++){
        d=((double)rand())/RAND_MAX; if (d>=0.300000 && d<=0.500000)    {
            printf("--------d = %f ----------\n",d);
            nCount++;
        }
        printf("d = %f \n",d);
    }
    printf("符合要求的数值有%d个\n",nCount);
    system("pause"); return 0;
}


Reproduced below



http://blog.csdn.net/beyond0824/article/details/6009908



How to generate random numbers in C/s + +: The rand () function, the Srand () function, and the random (int number) function in the C language/c++ are used here.
(1) If you want to generate random numbers without setting the range, you can just use RAND (): rand () returns a random number, ranging from 0 to Rand_max. Rand_max is defined in stdlib.h with a value of 2147483647.
For example:


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


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


For example: Randomly generate 10 0~100 numbers:
#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));
}





(3) But the random number generated by the above two examples can only be a one-time, if you run the second time the output will be the same as the first time. This is related to the Srand () function. The Srand () is used to set the random number seed when rand () produces a random number. Before calling the rand () function to produce a random number, the random number seed (seed) must be set with Srand (), and if no random number seed is set, rand () automatically sets the random number seed to 1 when called. The two examples above are that because no random number seed is set, each random number seed is automatically set to the same value of 1, which results in the same random values generated by rand ().



Srand () function definition: void srand (unsigned int seed);
You can usually use the return value of Geypid () or time (0) as the seed
If you use Time (0), add the header file #include<time.h>


For example:
#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));
}


So the results of two runs will be different!!






< two >



The function RAND () in the standard C library can generate a random number between 0~rand_max, where Rand_max is an integer defined in Stdlib.h, which is related to the system.



The rand () function does not have an input parameter and is directly referenced by the expression rand (), for example, you can print two random numbers using the following statement:



printf ("Random numbers is:%i%i/n", Rand (), Rand ());



Because the rand () function produces integers in the order specified, each execution of the above statement prints the same two values, so that the C language is not random in the true sense.



In order for the program to generate random values for a new sequence each time it executes, we usually provide a new random seed for the random number generator. The function Srand () (from stdlib.h) can spread the seed for the random number generator. As long as the seed is different, the rand () function produces a different sequence of random numbers. Srand () is called the initializer of the random number generator.



Routines:



File name: rand_srand.c



/* This program generates and prints ten random integers between 1 and rand_max*/



#include <stdio.h>



#includ <stdlib.h>



int main ()



{



usigned int seed; /* Declare the seed of the initializer, note the usigned int type */



int k;



Pringt ("Enter a positive integer seed value:/n");



scanf ("%u", &seed);



Srand (seed);



printf ("Random Numbers are:/n");



for (k = 1; k <=; k++)



printf ("%i", Rand ());



printf ("/n");



return 0;



}



You will find that when you provide the seed in the same time, the sequence of random numbers is also the same. And when the seed is 1 o'clock, the same as when the Srand () function is not used, that is, the rand () function initializes the seed value to 1 by default;



The prototypes of these two functions in stdlib.h are:



int rand ();



void Srand (unsigned int);



Expand:



x = rand ()%11; /* Generates a random integer between 1~10 */



y = rand ()%51-25; /* Generate a random integer between 25 ~ 25 */



z = (double) rand ()/rand_max) * (B-A) + a;/* generation interval [b] random number */



Random number between 0-1 C + +


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.