【问题标题】:How to generate a random number within a range once in C?如何在C中一次生成一个范围内的随机数?
【发布时间】:2015-03-03 07:08:31
【问题描述】:

C 编程新手,我试图生成一个随机数一次。但是当程序遵守时,它会在用户指定的范围内生成 30 个随机数。使用数组会更容易吗?任何帮助都会很好。

编辑

程序应该提示用户在 1 到 10000 的范围内输入要模拟的销售数量。接下来,它应该对每个模拟销售执行以下操作:选择一个随机员工来记入销售,随机确定销售的总金额,将该金额添加到所选员工的运行销售总额中,并增加员工所做的销售数量。- 这基本上就是我想要做的。很抱歉造成混乱

#include<stdio.h>
#include<time.h>
#include<stdlib.h>

int main (void)
{
    int n,c; // n is the number of sales to simulate, c is the counter
    int id[30];// id[30] is the number of employees 
    srand(time(NULL));
    int myVar;

    printf("Enter the number of sales to simulate: ");
    scanf("%d", &n);
    while(n<1 || n>10000)//blocking while loop
    {
        //prompt user to enter proper number for calculation
        printf("Error: Enter a proper number in the range from 1 to 10000: \a");
        scanf("%d",&n);
    }
    printf("Id\n");//prints out the id
    for (c = 0; c<30; c++)
    {
        id[c] = c;
        printf("%d: ",id[c]); //prints out the id numbers starting from 0 and ending at 29
        myVar = rand() % n + 1;//prints random number sale ranging from 1 to n for each employee but needs to print once for a random employee ranging from 1 to n. And should print zeros for the rest of the employees. 
        printf("\t%d\n", myVar);
    }

    return 0;
}

【问题讨论】:

  • 检查How to generate a random number in C?。还有随机数范围的答案。
  • 好的,现在说明你的具体问题是什么(提供更多细节)。
  • 我正在尝试将随机数提供给随机员工 (0-30)。其余员工得零。这就是我使用 for 循环的原因。但它给每个员工一个随机数。那是我的问题
  • 所以,你想给一个随机的员工一个随机数并给其余的 0,但是你的代码给了每个员工一个随机数。那是对的吗 ?您必须发布其余代码,以便我们更好地理解并帮助您编写代码。
  • 是的,就是这样

标签: c arrays random range srand


【解决方案1】:

随机数生成代码在运行 31 次的循环内。

for (c = 0; c<=30; c++)   //c = 0 to 31 -> runs 31 times
{
  myVar = rand() % n + 1;
  printf("%d\n", myVar);
}

如果您希望随机数只生成一次,请删除for loop

【讨论】:

    【解决方案2】:
    myVar = rand() % n + 1;
    

    这是在运行 30 次的 for 循环中

    【讨论】:

    • 我知道,但我试图生成一个随机数一次。我的程序应该提示用户在 1 到 10000 的范围内输入要模拟的销售数量。接下来,它应该对每个模拟销售执行以下操作:选择一个随机员工来记入销售,随机确定总美元金额销售,将该美元金额添加到所选员工的运行销售总额中,并增加该员工的销售数量。
    【解决方案3】:

    你想做什么? 如果您只想打印一次数字,只需省略 for 循环:

    #include<stdio.h>
    #include<time.h>
    #include<stdlib.h>
    
    int main (void)
    {
    int n,c;
    srand(time(NULL));
    int myVar;
    
    printf("Enter the number of sales to simulate: ");
    scanf("%d", &n);
    while(n<1 || n>10000)//blocking while loop
    {
        //prompt user to enter proper number for calculation
        printf("Error: Enter a proper number in the range from 1 to 10000: \a");
        scanf("%d",&n);
    }
    
    myVar = rand() % n + 1;
    printf("%d\n", myVar);
    

    【讨论】:

      【解决方案4】:

      使用,

      srand ( time(NULL) );
      number = rand() % 30 + 1;
      

      或者如果想要生成唯一的随机数,请使用 github 中的 urand() 库。

      【讨论】:

        【解决方案5】:

        问题是你正在使用一个 for 循环,它在范围内创建 31 个(因为 c = 0 ; c &lt;= 30 )随机数并打印所有 31 个。如果你只想打印 1 个随机数,只需删除 for 循环。在这段代码中你实际上不需要myVar。您可以直接打印随机数而不存储它。就是这样

        #include<stdio.h>
        #include<time.h>
        #include<stdlib.h>
        
        int main (void)
        {
        int n;
        srand(time(NULL));
        
        
        printf("Enter the number of sales to simulate: ");
        scanf("%d", &n);
        while(n<1 || n>10000)//blocking while loop
        {
            //prompt user to enter proper number for calculation
            printf("Error: Enter a proper number in the range from 1 to 10000: \a");
            scanf("%d",&n);
        }
        
            // Removed the for loop and now only one random number is found
        
            printf("%d\n", rand() % n + 1);  // See, no need of a variable
        
        }
        

        另外,我不明白您所说的使用数组是什么意思,所以请详细说明您的想法。

        【讨论】:

          猜你喜欢
          • 2019-10-30
          • 2018-04-08
          • 1970-01-01
          • 2011-08-02
          • 1970-01-01
          • 1970-01-01
          • 2014-01-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多