方法越多也好,谢谢!
9 个解决方案
#1
快速产生?还得一个一个的生成吧
#2
#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() );
}
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() );
}
#3
送你一个以前写的程序,采用线性同余法(LCG)产生随机数,满足均匀分布。
#define MODLUS 2147483647
#define MULT1 24112
#define MULT2 26143
/* Set the default seeds for all 100 streams. */
static long zrng[] =
{ 1,
1973272912, 281629770, 20006270,1280689831,2096730329,1933576050,
913566091, 246780520,1363774876, 604901985,1511192140,1259851944,
824064364, 150493284, 242708531, 75253171,1964472944,1202299975,
233217322,1911216000, 726370533, 403498145, 993232223,1103205531,
762430696,1922803170,1385516923, 76271663, 413682397, 726466604,
336157058,1432650381,1120463904, 595778810, 877722890,1046574445,
68911991,2088367019, 748545416, 622401386,2122378830, 640690903,
1774806513,2132545692,2079249579, 78130110, 852776735,1187867272,
1351423507,1645973084,1997049139, 922510944,2045512870, 898585771,
243649545,1004818771, 773686062, 403188473, 372279877,1901633463,
498067494,2087759558, 493157915, 597104727,1530940798,1814496276,
536444882,1663153658, 855503735, 67784357,1432404475, 619691088,
119025595, 880802310, 176192644,1116780070, 277854671,1366580350,
1142483975,2026948561,1053920743, 786262391,1792203830,1494667770,
1923011392,1433700034,1244184613,1147297105, 539712780,1545929719,
190641742,1645390429, 264907697, 620389253,1502074852, 927711160,
364849192,2049576050, 638580085, 547070247 };
/* Generate the next random number. */
float lcgrand(int stream)
{
long zi, lowprd, hi31;
zi = zrng[stream];
lowprd = (zi & 65535) * MULT1;
hi31 = (zi >> 16) * MULT1 + (lowprd >> 16);
zi = ((lowprd & 65535) - MODLUS) +
((hi31 & 32767) << 16) + (hi31 >> 15);
if (zi < 0) zi += MODLUS;
lowprd = (zi & 65535) * MULT2;
hi31 = (zi >> 16) * MULT2 + (lowprd >> 16);
zi = ((lowprd & 65535) - MODLUS) +
((hi31 & 32767) << 16) + (hi31 >> 15);
if (zi < 0) zi += MODLUS;
zrng[stream] = zi;
return (zi >> 7 | 1) / 16777216.0;
}
void lcgrandst (long zset, int stream) /* Set the current zrng for stream
"stream" to zset. */
{
zrng[stream] = zset;
}
long lcgrandgt (int stream) /* Return the current zrng for stream "stream". */
{
return zrng[stream];
}
#define MODLUS 2147483647
#define MULT1 24112
#define MULT2 26143
/* Set the default seeds for all 100 streams. */
static long zrng[] =
{ 1,
1973272912, 281629770, 20006270,1280689831,2096730329,1933576050,
913566091, 246780520,1363774876, 604901985,1511192140,1259851944,
824064364, 150493284, 242708531, 75253171,1964472944,1202299975,
233217322,1911216000, 726370533, 403498145, 993232223,1103205531,
762430696,1922803170,1385516923, 76271663, 413682397, 726466604,
336157058,1432650381,1120463904, 595778810, 877722890,1046574445,
68911991,2088367019, 748545416, 622401386,2122378830, 640690903,
1774806513,2132545692,2079249579, 78130110, 852776735,1187867272,
1351423507,1645973084,1997049139, 922510944,2045512870, 898585771,
243649545,1004818771, 773686062, 403188473, 372279877,1901633463,
498067494,2087759558, 493157915, 597104727,1530940798,1814496276,
536444882,1663153658, 855503735, 67784357,1432404475, 619691088,
119025595, 880802310, 176192644,1116780070, 277854671,1366580350,
1142483975,2026948561,1053920743, 786262391,1792203830,1494667770,
1923011392,1433700034,1244184613,1147297105, 539712780,1545929719,
190641742,1645390429, 264907697, 620389253,1502074852, 927711160,
364849192,2049576050, 638580085, 547070247 };
/* Generate the next random number. */
float lcgrand(int stream)
{
long zi, lowprd, hi31;
zi = zrng[stream];
lowprd = (zi & 65535) * MULT1;
hi31 = (zi >> 16) * MULT1 + (lowprd >> 16);
zi = ((lowprd & 65535) - MODLUS) +
((hi31 & 32767) << 16) + (hi31 >> 15);
if (zi < 0) zi += MODLUS;
lowprd = (zi & 65535) * MULT2;
hi31 = (zi >> 16) * MULT2 + (lowprd >> 16);
zi = ((lowprd & 65535) - MODLUS) +
((hi31 & 32767) << 16) + (hi31 >> 15);
if (zi < 0) zi += MODLUS;
zrng[stream] = zi;
return (zi >> 7 | 1) / 16777216.0;
}
void lcgrandst (long zset, int stream) /* Set the current zrng for stream
"stream" to zset. */
{
zrng[stream] = zset;
}
long lcgrandgt (int stream) /* Return the current zrng for stream "stream". */
{
return zrng[stream];
}
#4
for(i=0;i<1000;i++)
{
rand_num[i]=rand()%79+1;
}
{
rand_num[i]=rand()%79+1;
}
#5
void rand_num(void)
{
int i,rand_num[1000]={0};
srand(time(0));
for(i=0;i<1000;i++)
{
rand_num[i] = rand()%79+1;
printf(" %d ",rand_num[i]);
if(i%10 == 0) printf("\n");
}
}
为什么搞得那么复杂?
接分...
{
int i,rand_num[1000]={0};
srand(time(0));
for(i=0;i<1000;i++)
{
rand_num[i] = rand()%79+1;
printf(" %d ",rand_num[i]);
if(i%10 == 0) printf("\n");
}
}
为什么搞得那么复杂?
接分...
#6
循环1000次
#7
c规定rand()可以产生不确定、无穷大数。
但是他提供了一个宏RAND_MAX是rand()的最大值
所以产生一定范围内的数可以这样做:
rand()/RAND_MAX*80
这个数最大值就是80了
但是他提供了一个宏RAND_MAX是rand()的最大值
所以产生一定范围内的数可以这样做:
rand()/RAND_MAX*80
这个数最大值就是80了
#8
随机数也是需要质量的。
譬如
混乱程度。
使用压缩算法时由随机数组成的文件不会减小。
样本的平方分布。
序列的相关系数。
楼主可以看 游戏编程精粹里的115页专门讨论随即数的生成,很经典。
譬如
混乱程度。
使用压缩算法时由随机数组成的文件不会减小。
样本的平方分布。
序列的相关系数。
楼主可以看 游戏编程精粹里的115页专门讨论随即数的生成,很经典。
#9
如果需要 发邮件给我 conquer@vip.sina.com 我把代码寄给你,还有检测程序。文件有点多,不好贴。
#1
快速产生?还得一个一个的生成吧
#2
#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() );
}
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() );
}
#3
送你一个以前写的程序,采用线性同余法(LCG)产生随机数,满足均匀分布。
#define MODLUS 2147483647
#define MULT1 24112
#define MULT2 26143
/* Set the default seeds for all 100 streams. */
static long zrng[] =
{ 1,
1973272912, 281629770, 20006270,1280689831,2096730329,1933576050,
913566091, 246780520,1363774876, 604901985,1511192140,1259851944,
824064364, 150493284, 242708531, 75253171,1964472944,1202299975,
233217322,1911216000, 726370533, 403498145, 993232223,1103205531,
762430696,1922803170,1385516923, 76271663, 413682397, 726466604,
336157058,1432650381,1120463904, 595778810, 877722890,1046574445,
68911991,2088367019, 748545416, 622401386,2122378830, 640690903,
1774806513,2132545692,2079249579, 78130110, 852776735,1187867272,
1351423507,1645973084,1997049139, 922510944,2045512870, 898585771,
243649545,1004818771, 773686062, 403188473, 372279877,1901633463,
498067494,2087759558, 493157915, 597104727,1530940798,1814496276,
536444882,1663153658, 855503735, 67784357,1432404475, 619691088,
119025595, 880802310, 176192644,1116780070, 277854671,1366580350,
1142483975,2026948561,1053920743, 786262391,1792203830,1494667770,
1923011392,1433700034,1244184613,1147297105, 539712780,1545929719,
190641742,1645390429, 264907697, 620389253,1502074852, 927711160,
364849192,2049576050, 638580085, 547070247 };
/* Generate the next random number. */
float lcgrand(int stream)
{
long zi, lowprd, hi31;
zi = zrng[stream];
lowprd = (zi & 65535) * MULT1;
hi31 = (zi >> 16) * MULT1 + (lowprd >> 16);
zi = ((lowprd & 65535) - MODLUS) +
((hi31 & 32767) << 16) + (hi31 >> 15);
if (zi < 0) zi += MODLUS;
lowprd = (zi & 65535) * MULT2;
hi31 = (zi >> 16) * MULT2 + (lowprd >> 16);
zi = ((lowprd & 65535) - MODLUS) +
((hi31 & 32767) << 16) + (hi31 >> 15);
if (zi < 0) zi += MODLUS;
zrng[stream] = zi;
return (zi >> 7 | 1) / 16777216.0;
}
void lcgrandst (long zset, int stream) /* Set the current zrng for stream
"stream" to zset. */
{
zrng[stream] = zset;
}
long lcgrandgt (int stream) /* Return the current zrng for stream "stream". */
{
return zrng[stream];
}
#define MODLUS 2147483647
#define MULT1 24112
#define MULT2 26143
/* Set the default seeds for all 100 streams. */
static long zrng[] =
{ 1,
1973272912, 281629770, 20006270,1280689831,2096730329,1933576050,
913566091, 246780520,1363774876, 604901985,1511192140,1259851944,
824064364, 150493284, 242708531, 75253171,1964472944,1202299975,
233217322,1911216000, 726370533, 403498145, 993232223,1103205531,
762430696,1922803170,1385516923, 76271663, 413682397, 726466604,
336157058,1432650381,1120463904, 595778810, 877722890,1046574445,
68911991,2088367019, 748545416, 622401386,2122378830, 640690903,
1774806513,2132545692,2079249579, 78130110, 852776735,1187867272,
1351423507,1645973084,1997049139, 922510944,2045512870, 898585771,
243649545,1004818771, 773686062, 403188473, 372279877,1901633463,
498067494,2087759558, 493157915, 597104727,1530940798,1814496276,
536444882,1663153658, 855503735, 67784357,1432404475, 619691088,
119025595, 880802310, 176192644,1116780070, 277854671,1366580350,
1142483975,2026948561,1053920743, 786262391,1792203830,1494667770,
1923011392,1433700034,1244184613,1147297105, 539712780,1545929719,
190641742,1645390429, 264907697, 620389253,1502074852, 927711160,
364849192,2049576050, 638580085, 547070247 };
/* Generate the next random number. */
float lcgrand(int stream)
{
long zi, lowprd, hi31;
zi = zrng[stream];
lowprd = (zi & 65535) * MULT1;
hi31 = (zi >> 16) * MULT1 + (lowprd >> 16);
zi = ((lowprd & 65535) - MODLUS) +
((hi31 & 32767) << 16) + (hi31 >> 15);
if (zi < 0) zi += MODLUS;
lowprd = (zi & 65535) * MULT2;
hi31 = (zi >> 16) * MULT2 + (lowprd >> 16);
zi = ((lowprd & 65535) - MODLUS) +
((hi31 & 32767) << 16) + (hi31 >> 15);
if (zi < 0) zi += MODLUS;
zrng[stream] = zi;
return (zi >> 7 | 1) / 16777216.0;
}
void lcgrandst (long zset, int stream) /* Set the current zrng for stream
"stream" to zset. */
{
zrng[stream] = zset;
}
long lcgrandgt (int stream) /* Return the current zrng for stream "stream". */
{
return zrng[stream];
}
#4
for(i=0;i<1000;i++)
{
rand_num[i]=rand()%79+1;
}
{
rand_num[i]=rand()%79+1;
}
#5
void rand_num(void)
{
int i,rand_num[1000]={0};
srand(time(0));
for(i=0;i<1000;i++)
{
rand_num[i] = rand()%79+1;
printf(" %d ",rand_num[i]);
if(i%10 == 0) printf("\n");
}
}
为什么搞得那么复杂?
接分...
{
int i,rand_num[1000]={0};
srand(time(0));
for(i=0;i<1000;i++)
{
rand_num[i] = rand()%79+1;
printf(" %d ",rand_num[i]);
if(i%10 == 0) printf("\n");
}
}
为什么搞得那么复杂?
接分...
#6
循环1000次
#7
c规定rand()可以产生不确定、无穷大数。
但是他提供了一个宏RAND_MAX是rand()的最大值
所以产生一定范围内的数可以这样做:
rand()/RAND_MAX*80
这个数最大值就是80了
但是他提供了一个宏RAND_MAX是rand()的最大值
所以产生一定范围内的数可以这样做:
rand()/RAND_MAX*80
这个数最大值就是80了
#8
随机数也是需要质量的。
譬如
混乱程度。
使用压缩算法时由随机数组成的文件不会减小。
样本的平方分布。
序列的相关系数。
楼主可以看 游戏编程精粹里的115页专门讨论随即数的生成,很经典。
譬如
混乱程度。
使用压缩算法时由随机数组成的文件不会减小。
样本的平方分布。
序列的相关系数。
楼主可以看 游戏编程精粹里的115页专门讨论随即数的生成,很经典。
#9
如果需要 发邮件给我 conquer@vip.sina.com 我把代码寄给你,还有检测程序。文件有点多,不好贴。