random是VB中的,randomize是TC中的 下面实现1-10之间的随机数#include <stdlib.h> #include <iostream.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 ) ); double p=0; for( i = 0; i < 100;i++ ) { p=rand()%10+1; cout<<p<<endl; }
srand( (unsigned)time( NULL ) ); // srand(rand());// int i = rand();
Example/* 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——摘自MSDN
下面实现1-10之间的随机数#include <stdlib.h>
#include <iostream.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 ) );
double p=0;
for( i = 0; i < 100;i++ )
{ p=rand()%10+1;
cout<<p<<endl;
}
srand(rand());//
int i = rand();
#include <stdlib.h> /* header for rand() and srand() */
#include <stdio.h> /* io header */
#include <time.h> /* header needed for time() */
void main()
{ srand(time(0));
for(int i=0;i<=100;i++)
printf("%d\n",rand());
}
int i = rand();
你还可以控制随机数的范围!
int i = rand()/%m+n;
那么随机数i的范围是n----m+n;
int i = rand();
int x = 100;
int i = rand()/x;
那么随机数i的范围是0---99;
//srand(m_Index);
for(i=0;i<n;i++)//换牌算法
{
dw3 = (dw1 + dw2) ^ dwMask;
RandNum = dw3%Number+1;
dw1 = dw2;dw2 = dw3;
}
Number是的随机数最大值
* 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——摘自MSDN
产生高级的请看http://sjdwy.533.net/zw/SJS.htm
unsigned int seed
);
Parameters
seed
Seed for random-number generation
Res
The srand function sets the starting point for generating a series of pseudorandom integers. To reinitialize the generator, use 1 as the seed argument. Any other value for seed sets the generator to a random starting point. rand retrieves the pseudorandom numbers that are generated. Calling rand before any call to srand generates the same sequence as calling srand with seed passed as 1.Requirements
Routine Required header Compatibility
srand <stdlib.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP For additional compatibility information, see Compatibility in the Introduction.LibrariesAll versions of the C run-time libraries.Example
/* 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
10104See Also
Floating-Point Support Routine | rand--------------------------------------------------------------------------------Send feedback to Microsoft© 2001 Microsoft Corporation. All rights reserved.