求助:怎么使用rand函数和srand函数产生随机数。

If_Nothing_ 2011-12-25 06:40:32
各位大侠,你们好,本人大一新生,刚刚开始接触C语言。遇见一个问题:定义两个变量例如m,n怎么才能将rand()函数产生的随机数赋值给这两个变量啊?我用的编译系统是Visual C++6.0.求各位大侠的帮助啊!!!
...全文
411 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2011-12-31
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 chendongbox 的回复:]
引用 7 楼 sdksc 的回复:

引用 2 楼 yuzhenhua001 的回复:
n = rand()%10;//产生0-10的随机数

是0到9。小白路过


这样产生的随机数严格说来上是不随机的,跟RAND_MAX有关,一般C中,RAND_MAX=32767,
这样rand()%10产生8 9的概率小一些。
[/Quote]
“rand()%10产生8 9的概率小一些”纯属无稽之谈。
赵4老师 2011-12-31
  • 打赏
  • 举报
回复
根据9楼srand和rand的源代码可知,srand只需且必须在程序初始化时调用一次。
搜“线性同余法”
螃蟹虾米 2011-12-31
  • 打赏
  • 举报
回复
rand()产生的随机数其实是伪随机数,所以要播种,是吗?他们两个数一定要同时使用吗?[Quote=引用 10 楼 mscf 的回复:]

srand播种,rand收割,产生的随机数处于一定的区间,要扩散区间可以乘以一个大树获得~
[/Quote]
wizard_tiger 2011-12-31
  • 打赏
  • 举报
回复

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main()
{
int m, n;
srand((unsigned) time(NULL));
m = rand();
n = rand();
printf("m = %d\nn = %d\n", m, n);
return 0;
}

这个就可以了。
jiutianc 2011-12-31
  • 打赏
  • 举报
回复
++[Quote=引用 1 楼 udbwcso 的回复:]

C/C++ code

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(void)
{
int m, n;
time_t t;
srand((unsigned) time(&t));
m = rand();
n = rand();
printf("m = ……
[/Quote]
chendongbox 2011-12-30
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 sdksc 的回复:]

引用 2 楼 yuzhenhua001 的回复:
n = rand()%10;//产生0-10的随机数

是0到9。小白路过
[/Quote]

这样产生的随机数严格说来上是不随机的,跟RAND_MAX有关,一般C中,RAND_MAX=32767,
这样rand()%10产生8 9的概率小一些。
赵4老师 2011-12-30
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 if_nothing_ 的回复:]
引用 3 楼 cfjtaishan 的回复:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(void)
{
int m, n;
srand(time(NULL));
m = rand() % 100; //这是求0 ~100以内的随机数
n = rand() % 100; //这是求0 ~100……
[/Quote]
随机必然有重复,所谓“不重复的随机”不是随机,而是洗牌。
洗牌请参考下面:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int d[6];
int i,n,a,b,t;
int c,j;
void main() {
srand(time(NULL));
printf("shuffle 0..n-1 demo\n");
for (n=1;n<=5;n++) {/* 测试1~5个元素 */
printf("_____n=%d_____\n",n);
j=1;
for (c=1;c<=n;c++) j=j*c;/* j为n! */
j*=n*2;
for (c=1;c<=j;c++) {/* 测试n*2*n!次 */
for (i=0;i<n;i++) d[i]=i;/* 填写0~n-1 */
for (i=n;i>0;i--) {/* 打乱0~n-1 */
a=i-1;b=rand()%i;
if (a!=b) {t=d[a];d[a]=d[b];d[b]=t;}
}
printf("%04d:",c);
for (i=0;i<n;i++) printf("%d",d[i]);
printf("\n");
}
}
printf("shuffle 1..n demo\n");
for (n=1;n<=5;n++) {/* 测试1~5个元素 */
printf("_____n=%d_____\n",n);
j=1;
for (c=1;c<=n;c++) j=j*c;/* j为n! */
j*=n*2;
for (c=1;c<=j;c++) {/* 测试n*2*n!次 */
for (i=1;i<=n;i++) d[i]=i;/* 填写1~n */
for (i=n;i>1;i--) {/* 打乱1~n */
a=i;b=rand()%i+1;
if (a!=b) {t=d[a];d[a]=d[b];d[b]=t;}
}
printf("%04d:",c);
for (i=1;i<=n;i++) printf("%d",d[i]);
printf("\n");
}
}
}
shangwu_35 2011-12-30
  • 打赏
  • 举报
回复
简单的办法可以判断一下,不过效果不好。
这几个函数单纯用的话规律性太强,都是伪随机,
如果你要随机效果好点的,恐怕只能自己来写喽。随机数本身就是一门学科。
随机好不好的判定标准就是在长时间输出的情况下,重现的概率是不是足够低,越低也就越接近真随机,
人为的只能接近真随机,真随机也只有在具有无限参量---线性、非线性,输入的自然界才存在。
SDKSC 2011-12-26
  • 打赏
  • 举报
回复
只用
rand();
这个函数产生的数并不随机。比如你第一次产生的是25,,3,4,58,6
你再次运行程序,他还是产生25,,3,4,58,6
所以才用srand(time(0));来对他初始化,因为系统时间随时都在变的
SDKSC 2011-12-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 yuzhenhua001 的回复:]
n = rand()%10;//产生0-10的随机数
[/Quote]
是0到9。小白路过
卡卡_苏米 2011-12-26
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 c87527124 的回复:]
C/C++ code

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int a;
srand(time(0));//设置种子,产生随机数。种子为系统时间。
a=rand();
printf("%d",a);
}
[/Quote]

简单易懂
If_Nothing_ 2011-12-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 cfjtaishan 的回复:]
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(void)
{
int m, n;
srand(time(NULL));
m = rand() % 100; //这是求0 ~100以内的随机数
n = rand() % 100; //这是求0 ~100以内的随机数
print……
[/Quote]
我试过这样的;
for(int i=0;i<10;i++)
printf("%d %d\n",m,n);
但是产生的随机数m,n都一样啊!请问有什么方法可以得到随机数每一次都不一样啊?
薛定谔之死猫 2011-12-26
  • 打赏
  • 举报
回复
srand播种,rand收割,产生的随机数处于一定的区间,要扩散区间可以乘以一个大树获得~
赵4老师 2011-12-26
  • 打赏
  • 举报
回复
C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\rand.c
/***
*rand.c - random number generator
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines rand(), srand() - random number generator
*
*******************************************************************************/

#include <cruntime.h>
#include <mtdll.h>
#include <stddef.h>
#include <stdlib.h>

/***
*void srand(seed) - seed the random number generator
*
*Purpose:
* Seeds the random number generator with the int given. Adapted from the
* BASIC random number generator.
*
*Entry:
* unsigned seed - seed to seed rand # generator with
*
*Exit:
* None.
*
*Exceptions:
*
*******************************************************************************/

void __cdecl srand (
unsigned int seed
)
{
_getptd()->_holdrand = (unsigned long)seed;
}


/***
*int rand() - returns a random number
*
*Purpose:
* returns a pseudo-random number 0 through 32767.
*
*Entry:
* None.
*
*Exit:
* Returns a pseudo-random number 0 through 32767.
*
*Exceptions:
*
*******************************************************************************/

int __cdecl rand (
void
)
{
_ptiddata ptd = _getptd();

return( ((ptd->_holdrand = ptd->_holdrand * 214013L
+ 2531011L) >> 16) & 0x7fff );
}
c87527124 2011-12-25
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int a;
srand(time(0));//设置种子,产生随机数。种子为系统时间。
a=rand();
printf("%d",a);
}
Chosen_J 2011-12-25
  • 打赏
  • 举报
回复
rand函数要求添加stdlib.h(标准库,standard library)头文件
rand()用括号内的值产生随机数字
srand()来设置实际数产生器种子
此处以时间为种子

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(void)
{
int m, n;
time_t t;
srand((unsigned) time(&t));
m = rand();
n = rand();
printf("m = %d\nn = %d\n", m, n);
return 0;
自信男孩 2011-12-25
  • 打赏
  • 举报
回复
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(void)
{
int m, n;
srand(time(NULL));
m = rand() % 100; //这是求0 ~100以内的随机数
n = rand() % 100; //这是求0 ~100以内的随机数
printf("m = %d\nn = %d\n", m, n);
return 0;
}
鲤鱼 2011-12-25
  • 打赏
  • 举报
回复
n = rand()%10;//产生0-10的随机数
尘缘udbwcso 2011-12-25
  • 打赏
  • 举报
回复

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(void)
{
int m, n;
time_t t;
srand((unsigned) time(&t));
m = rand();
n = rand();
printf("m = %d\nn = %d\n", m, n);
return 0;
}


69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧