在C中生成随机数 [英] Generate random numbers in C

查看:76
本文介绍了在C中生成随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个菜单[menu1,menu2]我想根据生成的随机数(1或2)为这两个菜单选择不同的名称,所以,我使用了menu1 [r1] .c1 [r1 ]和menu2 [r2] .c2 [r2]检查它是否等于零在这种情况下我选择名称格式menu1 [r1] .n1 [r1]和menu2 [r2] .n2 [r2]。

所以如果所有menu1 [r1] .c1 [r1] = 1,程序必须生成不同的随机数,输出必须是mee1,mee2,me16,mee4,mee5,me13,mee7,mee8 ,mee9,me10,me11,me12,mee6,me14,me15,mee3



或根据生成的随机数进行不同安排



我的尝试:



---

Hi,I have two menus [menu1, menu2] I want to choose different names for these two menus according to the random number was generated(either 1 or 2), so, I used menu1[r1].c1[r1] and menu2[r2].c2[r2] to check if it is equal to zero in this case I choose name form menu1[r1].n1[r1] and menu2[r2].n2[r2].
So if all menu1[r1].c1[r1] =1 ,the program must generate different random number, the output must be mee1,mee2,me16,mee4,mee5,me13,mee7,mee8,mee9,me10,me11,me12,mee6,me14,me15,mee3

or in different arrange according to the random number that generates

What I have tried:

---

推荐答案

要生成1或2的随机数,您可以使用以下代码:

To generate a random number either 1 or 2, you can use the following code:
#include <stdlib.h>

int random_number = (rand() % 2) + 1;





希望这有帮助。



Hope this helps.


我想我几乎明白你想做什么,但是我墨水你不能按照编码的方式做到(见我的评论)。

问题是:如果你随机抽取一个数字(1或2)16次,没有什么可以保证你会有同样的一两个的数量。即使通过在 c 成员变量中设置标志,也会完全混淆算法。我想有一个16个名字的阵列,你会洗牌会更干净。

你可以这样做:

I think I almost understood what you want to do, but I think you cannot do it the way you have coded (see my comment).
The problem is: if you take a random number (1 or 2) 16 times, nothing guarantees that you will have the same number of ones and twos. Even by setting a flag in the c member variable as you did, that completely messes up with the algorithm. I think having an array of 16 names that you would shuffle would be much cleaner.
You could do this way:
#include <time.h>

struct f
{
   const char* name;
};

// Shuffles an array
void shuffle(f menu[], size_t count);

// The size of the array
const int SIZE = 16;

int main() {
   // Declarations
   int i;
   struct f menu[SIZE];

   // Initializations
   menu[ 0].name = "mee1";
   menu[ 1].name = "mee2";
   menu[ 2].name = "mee3";
   menu[ 3].name = "mee4";
   menu[ 4].name = "mee5";
   menu[ 5].name = "mee6";
   menu[ 6].name = "mee7";
   menu[ 7].name = "mee8";
   menu[ 8].name = "mee9";
   menu[ 9].name = "me10";
   menu[10].name = "me11";
   menu[11].name = "me12";
   menu[12].name = "me13";
   menu[13].name = "me14";
   menu[14].name = "me15";
   menu[15].name = "me16";

   // Shuffle the numbers
   shuffle(menu, SIZE);
}

// Algorithm from https://benpfaff.org/writings/clc/shuffle.html
void shuffle(f menu[], size_t count) {
   size_t i, j;
   f t;
   srand((unsigned int)time(NULL));
   if (count > 1) {
      for (i = 0; i < count - 1; ++i) {
         j = i + rand() / (RAND_MAX / (count - i) + 1);
         t = menu[j];
         menu[j] = menu[i];
         menu[i] = t;
      }
   }
}



我改变了你的原始算法,但那是因为我不认为你能做到这一点你做的方式。我保留了 f 结构,但摆脱了 str1 str2 因为他们只引入了一些不必要的复杂性。

我知道这是一个很大的变化,并且不知道确切的要求这可能不是一个有效的解决方案。如果它无效,我很抱歉。我不认为我只能稍微修改你的解决方案并获得相同的结果。



正如我所说的,我没有C编译器。我在VS 2017 C ++项目上进行了测试,实际上我得到了预期的名称。


I changed your original algorithm a lot, but that is because I do not think you could achieve this the way you had done it. I kept the f structure, but got rid of str1 and str2 as they only introduced some unnecessary complexity.
I am aware this is a big change, and not knowing the exact requirement this may not be a valid solution. If it is not valid, my apologies. I don't think I would be able to only slightly modify your solution and achieve the same result.

As I said, I do not have a C compiler. I tested it on a VS 2017 C++ project and I actually get expected shuffling of names.


这篇关于在C中生成随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆