【C】初学习之库函数——通用的实用工具<stdlib.h>之伪随机序列生成函数

rand函数和srand函数都可以用来生成伪随机数。(用于模拟程序和玩游戏程序)

(1)rand函数——随机数发生器

每次调用rand函数,会返回一个0—RAND_MAX(<stdlib.h>中定义的宏)的数字。rand函数返回的数字是由“种子”产生的,所以实际上其返回的数值并不是真正的随机数。但对于偶然的观察者来说.rand函数似乎能产生不相关的数值序列。

用法:int rand(void) ;

(2)srand函数——初始化种子值

调用srand函数可以为rand函数提供种子值。如果在srand函数之前调用rand函数。那么会把种子值定为1。每个种子确定一个特定的伪随机序列,用户可以自己选择数值序列。

用法:void srand(unsigned int seed);

(3)time函数——产生和时间有关的随机种子

始终使用同一种子值会使得rand函数得到相同的数值序列,但是如果用户想模拟随机序列,即每次得到不同的序列,即必须使种子值“随机化”。最简单的方式是调用time函数。它返回一个对当前时间编码后得到的数值,并返回传递给srand函数,这样rand函数每次运行的时间都不相同。

用法:srand ((unsigned int) time (NULL));

(4)产生一定范围通用随机数范围的通用公式

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


下面看两个例子:


(1)测试伪随机序列生成函数

// Tests the pseudo-random sequence generation functions
#include
   
   
    
    
#include
    
    
     
     

int main(void) {
	int i, seed;
	printf("This program displays the first five values of rand.\n");

	for (;;) {
		for (i = 0; i < 5; i++)
			printf("%d ", rand());
		printf("\n\n");
		printf("Enter new seed value(0 to terminate): ");
		scanf_s("%d", &seed);

		if (seed == 0)
			break;
		srand(seed);
	}
	getch();
	return 0;
}
    
    
   
   
程序运行结果如下所示:

        

该程序首先返回rand函数的前5个值,然后用户选择新的种子值,遇到0程序结束。从运行程序结果来看,我们可知选择1为种子值和不指定种子值所得的数列相同


(2)模拟“扎金花”游戏发牌,得到三张牌的结果(利用time函数生成随机种子)

// Tests the pseudo-random sequence generation functions
#include
   
   
    
    
#include
    
    
     
     
#include
     
     
      
      

#define Cards 12
#define Colors 4

int straight(int a[3]);

int main() {
	int card[3];
	int color[3];
	srand((unsigned)time(NULL));
	for (int i = 0; i < 3; i++) {
		card[i]= rand() % Cards + 1;
		color[i] = rand() % Colors + 1;
		switch (color[i])
		{
		case 1:  printf("The %d cards is spade %d\n", i+1,card[i]); break;
		case 2:  printf("The %d cards is heart %d\n", i+1, card[i]); break;
		case 3:  printf("The %d cards is club %d\n", i+1, card[i]); break;
		case 4:  printf("The %d cards is diamond %d\n", i+1, card[i]); break;
		}
	}
	if ((color[0] == color[1]) && (color[1] == color[2]))
		printf("golden flower !\n");
	else if ((card[0] == card[1]) && (card[1] == card[2]))
		printf("leaopord !\n");
	else if ((card[0] == card[1]) || (card[1] == card[2]) || (card[0] == card[2]))
		printf("pair !\n");
	else if (straight(card) == 1)
		printf("straight!\n");
	else
		printf("single!\n");
	getch();
	return 0;
}

int straight(int a[3]) {
	int max=a[0],  min=a[2];
	for (int i = 0; i < 3; i++) {
		if (a[i] > max){
			max = a[i];
		}
		if (a[i] < min) {
			min = a[i];
		}
	}
	if (max - min == 2) { return 1; }
	else { return -1; }
}
     
     
    
    
   
   
该程序第14行利用time函数生成随机种子,并返回给rand生成随机数序列。来模拟纸牌游戏“扎金花”的牌。
        ——————
如有不足请多多指教。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值