KNOW: rand() function & Card Shuffle Algorithm

计算机的随机数都是由伪随机数,即是由小M多项式序列生成的,其中产生每个小序列都有一个初始值,即随机种子。(注意: 小M多项式序列的周期是65535,即每次利用一个随机种子生成的随机数的周期是65535,当你取得65535个随机数后它们又重复出现了。

我们知道rand()函数可以用来产生随机数,但是这不是真正意义上的随机数,是一个伪随机数,是根据一个数(我们可以称它为种子)为基准以某个递推公式推算出来的一系列数,当这系列数很大的时候,就符合正态公布,从而相当于产生了随机数,但这不是真正的随机数,当计算机正常开机后,这个种子的值是定了的,除非你破坏了系统。

1.rand()

功能:随机数发生器

用法:int rand(void)

所在头文件: stdlib.h

rand()的内部实现是用线性同余法做的,它不是真的随机数,因其周期特别长,故在一定的范围里可看成是随机的。

rand()返回一随机数值的范围在0至RAND_MAX 间。RAND_MAX的范围最少是在32767之间(int)。用unsigned int 双字节是65535,四字节是4294967295的整数范围。0~RAND_MAX每个数字被选中的机率是相同的。

用户未设定随机数种子时,系统默认的随机数种子为1。rand()产生的是伪随机数字,每次执行时是相同的;若要不同,用函数srand()初始化它。

2.srand()

功能:初始化随机数发生器

用法: void srand(unsigned int seed)

所在头文件: stdlib.h

srand()用来设置rand()产生随机数时的随机数种子。参数seed必须是个整数,如果每次seed都设相同值,rand()所产生的随机数值每次就会一样。

3.使用当前时钟作为随机数种子

rand()产生的随机数在每次运行的时候都是与上一次相同的。若要不同,用函数srand()初始化它。可以利用srand((unsigned int)(time(NULL))的方法,产生不同的随机数种子,因为每一次运行程序的时间是不同的。

time(NULL): The value returned generally represents the number of seconds since 00:00 hours, Jan 1, 1970 UTC (i.e., the currentunix timestamp).

time function: http://www.cplusplus.com/reference/ctime/time/

4.产生随机数的用法

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

0~RAND_MAX之间的随机数程序

#include <iostream>
#include <stdlib.h>
#include <time.h> 
using namespace std; 
int main()
{ 
srand((unsigned)time(NULL)); 
for(int i = 0; i < 10;i++ ) 
        cout << rand() << '\t'; 
cout << endl; 
return 0;
}

5.产生一定范围随机数的通用表示公式

要取得[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)。

6. Sample Code

每次运行程序产生同样的随机数(种子始终是1):

#include <iostream>
#include <stdlib.h>
#include <time.h> 
using namespace std; 
int main()
{ 
//srand((unsigned)time(NULL)); 
for(int i = 0; i < 10;i++ ) 
        cout << rand()%10 << '\t'; 
cout << endl; 
return 0;
}
每次运行程序产生不同的随机数(种子不同):

#include <iostream>
#include <stdlib.h>
#include <time.h> 
using namespace std; 
int main()
{ 
srand((unsigned)time(NULL)); 
for(int i = 0; i < 10;i++ ) 
        cout << rand()%10 << '\t'; 
cout << endl; 
return 0;
}

7. 产生不重复随机数

1. Fisher-Yates shuffle

wiki

The basic method given for generating a random permutation of the numbers 1–N goes as follows:

  1. Write down the numbers from 1 to N.
  2. Pick a random number k between one and the number of unstruck numbers remaining (inclusive).
  3. Counting from the low end, strike out the kth number not yet struck out, and write it down elsewhere.
  4. Repeat from step 2 until all the numbers have been struck out.
  5. The sequence of numbers written down in step 3 is now a random permutation of the original numbers.

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

void swap(int *a, int *b)
{
    int c = *a;
    *a = *b;
    *b = c;
}

int main(int argc, const char * argv[])
{
    srand(time(NULL));
    int a[100];
    int i;
    for(i = 0; i <= 99; ++i)
        a[i] = i;
    for(i = 99; i >= 1; --i)
        swap(&a[i], &a[rand()%(i+1)]);   // swipe ai with a0 ~ ai, inclusive!!!
    for (i=0; i <= 99; ++i) {
        printf("%d\n", a[i]);
    }
}

* notice how to write swap function!

2. C++ std::random_shuffle

random_shuffle reference: http://www.cplusplus.com/reference/algorithm/random_shuffle/

// random_shuffle example
#include <iostream>     // std::cout
#include <algorithm>    // std::random_shuffle
#include <vector>       // std::vector
#include <ctime>        // std::time
#include <cstdlib>      // std::rand, std::srand

// random generator function:
int myrandom (int i) { return std::rand()%i;}

int main () {
  std::srand ( unsigned ( std::time(0) ) );
  std::vector<int> myvector;

  // set some values:
  for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9

  // using built-in random generator:
  std::random_shuffle ( myvector.begin(), myvector.end() );

  // using myrandom:
  std::random_shuffle ( myvector.begin(), myvector.end(), myrandom);

  // print out content:
  std::cout << "myvector contains:";
  for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
    std::cout << ' ' << *it;

  std::cout << '\n';

  return 0;
}

8. Reservoir sampling

http://en.wikipedia.org/wiki/Reservoir_sampling

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值