# C语言中随机数产生

int rand( void );

The rand function returns a pseudorandom integer in the range

0 to RAND_MAX. Use the srand function to seed the pseudorandom

-number generator before calling rand.

/* Maximum value returned by "rand" function
*/
#define RAND_MAX 0x7FFF

#include "stdlib.h"
#include "stdio.h"

void main( void )
{
/* Display a number. */
printf( " %6d/n", rand() );

getchar();
}

346

void srand( unsigned int seed );

srand uses seed(函数变量声明中的seed) as the seed（随机函数中种子

initial seed is 1.

/* RAND.C: This program seeds the random-number generator
* with the time, then displays 10 random integers.
*/

#include "stdlib.h"
#include "stdio.h"
#include "time.h"

void main( void )
{
int i;
/* Seed the random-number generator with current time so that
the numbers will be different every time we run.

*/
srand( (unsigned)time( NULL ) );
/* Display 10 numbers. */
for( i = 0; i < 10;i++ )
printf( “ %6d/n”, rand() );
}
Output

6929
8026
21987
30734
20587
6699
22034
25051
7988
10104

。下面是一个产生10以内随机数的函数：

#include "stdlib.h"
#include "stdio.h"
#include "time.h"

int rand2( void );

void main( void )
{
int i;
/* Seed the random-number generator with current time so that
· the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );
/* Display 10 numbers：0～9 */
for( i = 0; i < 10;i++ )
printf( " %6d/n", rand2() );

getchar();

}

int rand2( void )
{
return rand() % 10 ;
}

2
5
7
9
0
1
3
5
8
3

#define random(num) (rand() % (num))

#include "stdlib.h"
#include "stdio.h"
#include "time.h"

void main( void )
{
int i;
/* Seed the random-number generator with current time so that
· the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );
/* Display 10 numbers. */
for( i = 0; i < 10;i++ )
printf( " %6d/n", random( 10 ) );

getchar();

}

#define randomize() srand((unsigned)time(NULL))

#include "stdlib.h"
#include "stdio.h"
#include "time.h"

void main( void )
{
int i;
/* Seed the random-number generator with current time so that
· the numbers will be different every time we run.
*/
randomize();
/* Display 10 numbers. */
for( i = 0; i < 10;i++ )
printf( " %6d/n", random( 10 ) );

getchar();

}

/* 模拟随机数的产生过程
*/

#include "stdlib.h"
#include "stdio.h"
#include "time.h"

int randx = 0;

void srand2( int a );
int rand2( void );

void main( void )
{
int i;
int seed;
/* 输入不同的种子就可以产生不同的随机数
*/

printf( "Please input a seed: /n");
scanf(" %d",&seed);

srand2( seed );

getchar();

printf( " %d/n", rand2( ) );

getchar();

}

void srand2( int a )
{
randx = a;
}

int rand2()
{
return (int)( randx * 123265187.7795 + 569412.1256 ) ;
}

/* 模拟随机数的产生过程，以时间作为种子
*/

#include "stdlib.h"
#include "stdio.h"
#include "time.h"

int randx = 0;

void srand2( int a );
int rand2( void );

void main( void )
{
int i;
/* Seed the random-number generator with current time so that
· the numbers will be different every time we run.
*/

srand2( (unsigned)time( NULL ) );
/* Display 10 numbers. */
printf( " %6d/n", rand2( ) );

getchar();

}

void srand2( int a )
{
randx = a;
}

int rand2()
{
return (int)( randx * 123265187.7795 + 569412.1256 ) ;
}

1.函数rand()产生的是伪随机数，不是真正意义上的随机数，这个伪随机数

2.要产生真正意义上的随机数，要将函数srand( )和rand()配合使用，函数
srand（）用来设置随机数的种子，一般以时间作为种子，当然也有其它

3.设置随机数的种子，可以使用randomize()，它采用时间做为种子。
4.要产生给定范围的随机数，可以使用random（）。

SCR_VOID CW_GEN( )
{
int i;
srand( (unsigned)time( NULL ) );
int seed=rand();
srand(seed);
for( i = 0; i < 16;i++ )
{
buffer[i]=(char)(rand()%256);

}
printf( " 0x%02x/n", buffer[0]);
buffer[16]='/0';
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：C语言中随机数产生 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)