- EP 解决方案 " Erich Pul" < ER ******* @ blackbox.net>写道: 我有一个结构,应该填充随机整数值(它实际上是一个数字生成器,如在乐透中),但这些值不得重复出现,因此不需要重复出现。 我的代码: < code> Tipp * ziehung()// Tipp *是函数 不,ziehung()是函数。 Tipp *是其返回值的类型。 ziehungT.z1 =(rand()%45)+1; // struct'成员中的数据输入 ziehungT.z2 =(rand()%45)+1; ziehungT.z3 =(rand()%45)+1 ; ziehungT.z4 =(rand()%45)+1; ziehungT.z5 =(rand()%45)+1; ziehungT.z6 =(rand()% 45)+1; 那么那就不行了。 是否有可能规避打包数值为并运行几个数组的数组循环? 编号用1-45填充45个元素的数组有什么问题, 洗牌它(只需要一个单循环最多44次迭代 - 在 这种情况​​下你只需要6)然后取 洗牌数组的前6个元素? Richard > > Tipp * ziehung()// Tipp *是函数 不,ziehung()是函数。 Tipp *是其返回值的类型。 是的,我的意思是; ) [...] ziehungT.z4 =(rand()%45)+1; ziehungT.z5 =(rand()%45)+1; ziehungT.z6 =(rand()%45)+1; 所以赢了'那不行。 nah,这完全有效 - 问题是*可能*值*值b / b 出现两次或者更多(有时他们这样做,有时候没有) 编号用1-45填充45个元素的数组是否有任何问题,改组它(只需要一个最多44次迭代的循环 - 在这种情况下你只需要6)然后采用洗牌数组的前6个元素? 基本上没有,但这只是同一问题的一个变种, ,因为没有证据表明不会出现两次出现的价值。 问候, - Erich Pul Erich Pul写道: 不要剪辑归属(谁说的是什么)。这是由Richard Bos撰写的: No。用1-45填充45个元素的数组有什么问题,将它洗牌(只需要一次最多44次迭代的循环 - 在这种情况下你只需要6次)然后取出混洗数组的前6个元素? 基本上没有,但这只是同一问题的一个变种,因为没有证据表明不会出现两次出现的值。 我不认为你仔细阅读理查德的建议。保证 给出所有不同的数字。例如: 0 1 2 3 4 5 6 7 8 9 < shuffle> 1 7 3 5 6 2 4 9 8 0 <先取N> 可能最简单的洗牌方式是交换N对随机 选择的元素。 hi! i got a structure, which should be filled with random integer values(it is in fact a generator for numbers like in a lotto), but thesevalues must not be recurring, so no double occurrences are desired. my code: <code>Tipp* ziehung() // Tipp* is the function{Node *bewK; // a node that is my indexTipp ziehungT; // a new struct for storing the random valuesint x; x = 0;srand(time(NULL)); // init for the rand()while(x<=20) // for testing, gimme 20 values{ziehungT.z1 = (rand() % 45) +1; // data input in thestruct''s membersziehungT.z2 = (rand() % 45) +1;ziehungT.z3 = (rand() % 45) +1;ziehungT.z4 = (rand() % 45) +1;ziehungT.z5 = (rand() % 45) +1;ziehungT.z6 = (rand() % 45) +1; printf("\nGezogen wurden folgende Zahlen: %d %d %d %d %d %d",ziehungT.z1,ziehungT.z2,ziehungT.z3,ziehungT.z4,zi ehungT.z5,ziehungT.z6);x++;}} </code> is there a possibility to circumvent packing an array with the valuesand running through several loops? TIA,--EP 解决方案 "Erich Pul" <er*******@blackbox.net> wrote: i got a structure, which should be filled with random integer values (it is in fact a generator for numbers like in a lotto), but these values must not be recurring, so no double occurrences are desired. my code: <code> Tipp* ziehung() // Tipp* is the functionNo, ziehung() is the function. Tipp * is the type of its return value. ziehungT.z1 = (rand() % 45) +1; // data input in the struct''s members ziehungT.z2 = (rand() % 45) +1; ziehungT.z3 = (rand() % 45) +1; ziehungT.z4 = (rand() % 45) +1; ziehungT.z5 = (rand() % 45) +1; ziehungT.z6 = (rand() % 45) +1;So that won''t work, then. is there a possibility to circumvent packing an array with the values and running through several loops? No. Is there any problem with filling an array of 45 elements with 1-45,shuffling it (takes only a single loop of at most 44 iterations - inthis case you need only 6) and then taking the first 6 elements of theshuffled array? Richard> > Tipp* ziehung() // Tipp* is the function No, ziehung() is the function. Tipp * is the type of its return value.yep, i meant that ; ) [...] ziehungT.z4 = (rand() % 45) +1; ziehungT.z5 = (rand() % 45) +1; ziehungT.z6 = (rand() % 45) +1; So that won''t work, then. nah, that works perfectly - the problem is that there *MAY* be valuesthat occur twice or more (sometimes they do, sometimes not) No. Is there any problem with filling an array of 45 elements with 1-45, shuffling it (takes only a single loop of at most 44 iterations - in this case you need only 6) and then taking the first 6 elements of the shuffled array? basically not, but that is just a variation of the same problem,because there is no proof that there won''t be values that occur twice. greetings,--Erich Pul Erich Pul wrote: Don''t snip attributions (who said what). This was by Richard Bos: No. Is there any problem with filling an array of 45 elements with 1-45, shuffling it (takes only a single loop of at most 44 iterations - in this case you need only 6) and then taking the first 6 elements of the shuffled array? basically not, but that is just a variation of the same problem, because there is no proof that there won''t be values that occur twice. I don''t think you read Richard''s suggestion carefully. It is guaranteedto give all different numbers. E.g.: 0 1 2 3 4 5 6 7 8 9 <shuffle> 1 7 3 5 6 2 4 9 8 0 <take first N> Probably the easiest way to shuffle is to swap N pairs of randomlychosen elements. 这篇关于结构中的随机数据 - 检查没有double值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-14 00:31