• C++中 Rand随机序列函数


    标准库<cstdlib>(被包含于<iostream>中)提供两个帮助生成伪随机数的函数:  
            函数一:int rand(void);
    从srand (seed)中指定的seed开始,返回一个[seed, RAND_MAX(0x7fff))间的随机整数。  
            函数二:void srand(unsigned seed);

    参数seed是rand()的种子,用来初始化rand()的起始值。(一般情况下:我们都使用time(NULL)种子, 从一个时间点(一般是1970年1月1日0时0分0秒)到那时的秒数(即日历时间))  

    #define   RAND_MAX   0x7fffu   
    这个是bcc55中的定义,说明这个整数的最大数是0x7fffu,u代表unicode编码。

    PS: time(NULL)返回值是time_t,实际上是长整型,到未来的某一天。对于time_t数据类型的值来说,它所表示的时间不能晚于2038年1月18日19时14分07秒。不过基本上也够大家使用了,如果需要更长的时间,可以使用64的_time64来进行处理。


    rand()在每次被调用的时候,它会查看:

            1)如果用户在此之前调用过srand(seed),给seed指定了一个值,那么它会自动调用srand(seed)一次来初始化它的起始值。
            2)如果用户在此之前没有调用过srand(seed),它会自动调用srand(1)一次。  
                    根据上面的第一点我们可以得出:
                    1)如果希望rand()在每次程序运行时产生的值都不一样,必须给srand(seed)中的seed一个变值,这个变值必须在每次程序运行时都不一样(比如到目前为止流逝的时间)。
                    2)否则,如果给seed指定的是一个定值,那么每次程序运行时rand()产生的值都会一样,虽然这个值会是[seed, RAND_MAX(0x7fff))之间的一个随机取得的值。
                    3)如果在调用rand()之前没有调用过srand(seed),效果将和调用了srand(1)再调用rand()一样(1也是一个定值)。  

    产生随机数的用法
    1) 给srand()提供一个种子,它是一个unsigned int类型;
    2) 调用rand(),它会根据提供给srand()的种子值返回一个随机数(在0到RAND_MAX之间);
    3) 根据需要多次调用rand(),从而不间断地得到新的随机数;
    4) 无论什么时候,都可以给srand()提供一个新的种子,从而进一步“随机化”rand()的输出结果。

    一定范围随机数的通用表示公式

    • 要取得[a,b)的随机整数,使用(rand() % (b-a))+ a;
    • 要取得[a,b]的随机整数,使用(rand() % (b-a+1))+ a;
    • 要取得(a,b]的随机整数,使用(rand() % (b-a))+ a + 1;
    • 通用公式:a + rand() % n;其中的a是起始值,n是整数的范围。
    • 要取得a到b之间的随机整数,另一种表示:a + (int)b * rand() / (RAND_MAX + 1)。
    • 要取得0~1之间的浮点数,可以使用rand() / double(RAND_MAX)。


    MSDN标准示例代码

    1. // crt_rand.c  
    2. // This program seeds the random-number generator  
    3. // with the time, then exercises the rand function.  
    4. //  
    5.   
    6. #include <stdlib.h>  
    7. #include <stdio.h>  
    8. #include <time.h>  
    9.   
    10. void SimpleRandDemo( int n )  
    11. {  
    12.    // Print n random numbers.  
    13.    int i;  
    14.    for( i = 0; i < n; i++ )  
    15.       printf( "  %6d ", rand() );  
    16. }  
    17.   
    18. void RangedRandDemo( int range_min, int range_max, int n )  
    19. {  
    20.    // Generate random numbers in the half-closed interval  
    21.    // [range_min, range_max). In other words,  
    22.    // range_min <= random number < range_max  
    23.    int i;  
    24.    for ( i = 0; i < n; i++ )  
    25.    {  
    26.       int u = (double)rand() / (RAND_MAX + 1) * (range_max - range_min)  
    27.             + range_min;  
    28.       printf( "  %6d ", u);  
    29.    }  
    30. }  
    31.   
    32. int main( void )  
    33. {  
    34.    // Seed the random-number generator with the current time so that  
    35.    // the numbers will be different every time we run.  
    36.    srand( (unsigned)time( NULL ) );  
    37.   
    38.    SimpleRandDemo( 10 );  
    39.    printf(" ");  
    40.    RangedRandDemo( -100, 100, 10 );  

    1. }  
    2. from:http://blog.csdn.net/xiaoting451292510/article/details/17550161

    极简,专注,速度,极致
  • 相关阅读:
    HDU 4393 Throw nails(贪心加模拟,追及问题)
    【Add Two Numbers】
    【Single Num II】cpp
    【Single Number】cpp
    【Candy】cpp
    【Gas Station】cpp
    【Set Matrix Zeros】cpp
    【Gray Code】cpp
    【Climbing Stairs】cpp
    【Plus One】cpp
  • 原文地址:https://www.cnblogs.com/simplelifestyle/p/3761919.html
Copyright © 2020-2023  润新知