用c语言生成随机数的方法在c语言中,可以使用rand ) )函数生成随机数。 但是,这并不是真正意义上的随机数。 是伪随机数。 这是把某个数称为种子,用作为基准的递归公式可以算出的系数。 当这个系列的数量很大的时候,正式公布,生成了随机数,但这不是真正的随机数。 计算机正常启动后,此种子的值已经确定。 在不破坏系统的前提下,c提供srand (函数,其真实身份为voidsrand ) Inta )来改变此种子的值。
c语言的随机函数random可能是众所周知的,但由于random函数不是ANSIC标准,因此无法在gcc、vc等编译器下进行编译。
rand ) )返回0到RAND_MAX范围内的随机值。 返回0到RAND_MAX之间的随机数值。 RAND_MAX是在stdlib.h中定义的。 (那个值至少是32767。 我的运算结果是个不定的数字。 要查看你定义的变量类型,如果是int整数,则为32767。 在调用此函数生成随机数之前,必须使用srand ()设置随机数种子。 如果未设置随机数种子,rand ) )将在调用时自动将随机数种子设置为1。 通常用for语句设定种子的数量。 具体请参照以下示例。
一、如何建立不可预见的随机序列
使用srand((unsignedint ) (time ) (null ) )是因为每个程序运行的时间都不一样。
C语言提供的随机数生成器用法:当前的c编译器提供了基于ANSI标准的伪随机数生成器函数,用于生成随机数。 那些是rand (和srand )函数。 这两个函数的动作如下。
1 )首先为srand )提供种子。 这是无符号输入类型,取值范围为0到0~65535。
2 )然后调用rand ) ),根据srand ) )提供的种子值返回随机数(0到32767之间)。
3 )根据需要多次调用rand ),不间断地获得新的随机数
4 )在任何时候,可以向(srand ) )提供新种子以进一步“随机化”(rand ) )的输出结果。
以下是0到0~32767之间的随机数程序。
#includestdlib.h
#includestdio.h
#includetime.h//使用当前时钟作为种子
语音主(语音)。
{
inti;
srand () unsigned (time ) ) null ); //初始化随机数
for(I=0; i10; 打印I//10个随机数
打印(% d (n ),rand );
}
根据上述步骤,可以很容易地得到0到1之间的随机数。
#includestdlib.h
#includestdio.h
#includetime.h
main () )
{
inti;
srand () unsigned (time ) ) null );
for(I=0; i10; I )
printf(%5.2f(n ),rand ) )/32767.0 );
}
生成1到100之间的随机数后,可以写为:
#includestdlib.h
#includestdio.h
#includetime.h
main () )
{
inti;
srand () unsigned (time ) ) null );
for(I=0; i10; I )
printf(%d(n ),rand ) 01 );
}
2、三个通用随机数发生器,推荐第三个
函数名称:rand (
功能:随机数发生器
用法:voidrand(void;
程序示例:
#includestdlib.h
#includestdio.h
入主(void ) )。
{
inti;
class="p15"> printf("Ten random numbers from 0 to 99nn");for(i=0; i<10; i++)
printf("%dn", rand() % 100);
return 0;
}
函数名: random()
功能: 随机数发生器
用法: int random(int num);
程序例:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
/* prints a random number in the range 0 to 99 */
int main(void)
{
randomize();
printf("Random number in the 0-99 range: %dn", random (100));
return 0;
}
函数名: randomize() 这个比较好!
功 能: 初始化随机数发生器
用 法: void randomize(void);
程序例:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(void)
{
int i;
randomize();
printf("Ten random numbers from 0 to 99nn");
for(i=0; i<10; i++)
printf("%dn", rand() % 100);
return 0;
}
在《计算机常用算法》中有介绍随机数的生成算法
三、如何产生设定范围内的随机数
由于rand产生的随机数从0到rand_max,而rand_max是一个很大的数,那么如何产生从X~Y的数呢?
从X到Y,有Y-X+1个数,所以要产生从X到Y的数,只需要这样写:
k=rand()%(Y-X+1)+X;
这样,就可以产生你想要的任何范围内的随机数了。
四、产生不重复的随机数
1)
#include <stdlib.h>
#include <stdio.h>
#include<stdio.h>
#include <time.h>
swap(int *pm,int *pn)
{
int temp;
temp=*pm;
*pm=*pn;
*pn=temp;
}
int main(void)
{
int i,a[513];
/*int *pa,*pb;*/
srand( (unsigned)time( NULL ) ); /*定义这个可以产生不同的随机数*/
for(i=1; i<=512; i++){a[i]=i;printf("%4d",a[i]);}
for(i=512; i>=1; i--)
{
/* pa=&a[i]; pb=&a[rand()%i+1];*/
swap(&a[i], &a[rand()%i+1]); /*加一是从一到i的随机,就不会包含0*/
/*不用再定义指针,这样结论是一样的*/
}
printf("n") ;
for(i=1; i<=64; i++)
printf("%4d",a[i] );
getch(); /*wintc的输出*/
}
2)
#include <stdlib.h>
#include <stdio.h>
#include<stdio.h>
int main(void)
{
int a[100]={0}; int i,m;
for(i=1; i<=99; ++i)
printf("%4d",a[i] );
srand( (unsigned)time( NULL ) );
for(i=1; i<=99; i++)
{
while(a[m=rand()%100+1]);
a[m] = i;
}
for(i=1; i<=99; ++i)
printf("%4d",a[i] );
getch();
}
转载于:https://www.cnblogs.com/Camilo/p/3332279.html