如何在C范围内生成一个随机数?

问题描述:

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

编辑

程序应提示用户输入销售数量从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; 
} 
+0

检查[如何在C中生成一个随机数?](http://*.com/questions/822323/how-to-generate-a-random-number-in-c)。还有一些随机数字的答案。 – 2015-03-03 07:17:23

+1

好的,现在指定你确切的问题是什么(给予更多细节)。 – 2015-03-03 07:24:37

+0

即时尝试给随机数字给随机雇员(0-30)。其余员工得到零。那为什么即时通讯使用for循环。但是它给每个员工一个随机数。这就是我的问题 – nm10563 2015-03-03 07:31:44

用于随机数生成的代码位于运行31次的循环内部。

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

如果你想只有一次产生的随机数,去除for loop

myVar = rand() % n + 1; 

这里面有个针对运行30次

+0

我知道,但即时尝试产生一次随机数。我的程序应该提示用户输入销售数量,模拟范围从1到 10000.接下来,它应该为每个模拟销售做以下事情:选择一个随机员工以销售额为 ,随机确定总数将该美元金额添加到所选择的员工的运行销售总额中,并增加员工的销售数量。 – nm10563 2015-03-03 07:14:53

什么是你想要做循环? 如果你想只得到一次印刷数量只是省略了循环:

#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); 

那么问题是你使用的是for循环它创建的范围31(因为c = 0 ; c <= 30)随机数并打印所有31.如果你只想打印一个随机数,只需删除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 

} 

另外,我不明白你使用数组的意思,所以给你在想什么的更多细节。

使用,

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

,或者如果要生成唯一的随机号码,使用urand这是在github上发现的()库。