本文介绍了关于随机发生器的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 Hi Group, 跟着faq生成给定范围内的随机数,所以 使用了这个: #include< stdlib.h> #define ULIMIT 100 int(........) intarr [i ] =(rand()/(RAND_MAX / ULIMIT + 1)); 也许我没有运行足够的功能,但每次,intarr [0] 等于随后的数字是随机的。或者伪随机或伪随机。正如我想要的那样,但我无法解释元素''0'。我没有运行它吗?b $ b足够吗?还是有其他解释。 提前致谢。 Hi Group,Followed the faq to generate random numbers within a given range, soused this:# include <stdlib.h># define ULIMIT 100int(........) intarr[i]= ( rand()/(RAND_MAX/ULIMIT+1));Perhaps I have not run the function enough, but each time, intarr[0]equals 0. The subsequent numbers are "random" or "psuedo-random" as Iwould expect, but I cannot explain element ''0''. Have I just not run itenough? Or is there another explanation. Thanks in advance. 推荐答案 解释是在代码的其余部分,你没有发布。 如果你给我们展示一个完整的自包含程序,我们可以帮助你获得。否则,我们做的不仅仅是猜测。 - Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst> 圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst> 我们必须做点什么。这是事情。因此,我们必须这样做。 The explanation is in the rest of the code, which you didn''t post. If you''ll show us a complete self-contained program, we migh be ableto help. Otherwise, we can''t do more than guess. --Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>We must do something. This is something. Therefore, we must do this. 好​​的....我正在通过K& R,并试图让我的头围绕壳 排序。想要为我的预先排序生成随机数字。数组, " intarr"。但是只是使用rand()给了我这么大的数字,很难得到我的头脑。因此这个公式。函数的其余部分用排序处理 ,并且不影响intarr。 (我认为)。 #include< stdio.h> #include< stdlib.h> #define NELEMENTS 1000 #define UBOUND NELEMENTS-1 #define ULIMIT 100 int main(){ int intarr [NELEMENTS],v [NELEMENTS],i,j; for(i = 0; i< = UBOUND; i ++) intarr [i] =(rand()/(RAND_MAX / ULIMIT + 1)); j = i = 0; while(j< = UBOUND) v [j ++] = intarr [i ++]; Ok....I am going through K&R, and trying to get my head around ShellSorts. Wanted to generate random numbers for my "presorted" array,"intarr". But just using rand() gave me such big numbers, hard to getmy head around it. Hence this formula. The rest of the function dealswith the sort, and does not effect intarr. ( I think).#include <stdio.h># include <stdlib.h># define NELEMENTS 1000# define UBOUND NELEMENTS-1# define ULIMIT 100 int main (){ int intarr[NELEMENTS], v[NELEMENTS], i, j; for (i=0; i <=UBOUND; i++)intarr[i]= ( rand()/(RAND_MAX/ULIMIT+1)); j=i=0;while ( j<= UBOUND) v[j++] = intarr[i++]; 好的....我正在经历K& R,并试图让我的头围绕着壳牌排序。想要为我的预先排序生成随机数字。数组,intarr。但是只是使用rand()给了我这么大的数字,很难得到我的头脑。因此这个公式。函数的其余部分用排序处理,并且不影响intarr。 (我认为)。 #include< stdio.h> #include< stdlib.h> #define NELEMENTS 1000 #define UBOUND NELEMENTS- 1 #define ULIMIT 100 int main(){ int intarr [NELEMENTS],v [NELEMENTS],i,j; intarr [i] =(rand()/(RAND_MAX / ULIMIT + 1)); j = i = 0; while(j< = UBOUND) v [j ++] = intarr [i ++]; Ok....I am going through K&R, and trying to get my head around Shell Sorts. Wanted to generate random numbers for my "presorted" array, "intarr". But just using rand() gave me such big numbers, hard to get my head around it. Hence this formula. The rest of the function deals with the sort, and does not effect intarr. ( I think). #include <stdio.h> # include <stdlib.h> # define NELEMENTS 1000 # define UBOUND NELEMENTS-1 # define ULIMIT 100 int main (){ int intarr[NELEMENTS], v[NELEMENTS], i, j; for (i=0; i <=UBOUND; i++) intarr[i]= ( rand()/(RAND_MAX/ULIMIT+1)); j=i=0; while ( j<= UBOUND) v[j++] = intarr[i++]; 这还不完整。程序的其余部分在哪里? rand()总是生成相同的序列,给出相同的起始点 点。使用srand()指定不同的起始点。有关详细信息,请参阅 常见问题解答。我不知道这是不是问题。 - Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst> 圣地亚哥超级计算机中心< * > < http://users.sdsc.edu/~kst> 我们必须做点什么。这是事情。因此,我们必须这样做。 This is still incomplete. Where''s the rest of the program? rand() always generates the same sequence given the same startingpoint. Use srand() to specify a different starting point. See theFAQ for details. I don''t know if that''s the problem. --Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>We must do something. This is something. Therefore, we must do this. 这篇关于关于随机发生器的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-23 04:51