lundi 2 mars 2015

How to generate a random number within a range once in C?

New to programming in C and I'm trying to generate a random number once. But when the program complies it generates 30 random numbers in the range that the users specifies. Would using an Array be easier? Any help would be good.


edit


the program should prompt the user to enter the number of sales to simulate in the range from 1 to 10000. Next, it should do the following for each simulated sale: choose a random employee to credit the sale for, randomly determine the total dollar amount of the sale, add that dollar amount to the chosen employee's running sales total, and increment the number of sales that employee has made.- This is basically what i'm trying to do. Sorry for the confusion



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

for (c = 0; c<=30; c++)
{
myVar = rand() % n + 1;
printf("%d\n", myVar);
}




Aucun commentaire:

Enregistrer un commentaire