-1

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; // 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;
}
nm10563
  • 43
  • 1
  • 9
  • Check [How to generate a random number in C?](http://stackoverflow.com/questions/822323/how-to-generate-a-random-number-in-c). There are also answers for range of random numbers. – Anto Jurković Mar 03 '15 at 07:17
  • 1
    Okay, now specify what your exact problem is ( give more details ). – Arun A S Mar 03 '15 at 07:24
  • im trying give the random number to a random employee (0-30). The rest of the employees get zeros. that why im using the for loop. But its giving each employee a random number. That's my problem – nm10563 Mar 03 '15 at 07:31
  • So, you want to give a random employee a random number and give the rest 0, but your code gives every employee a random number. Is that correct ? You'll have to post the rest of your code so that we can understand better and help you with your code. – Arun A S Mar 03 '15 at 07:35
  • Yes, that is what is happening – nm10563 Mar 03 '15 at 07:40
  • So, do you need any more help ? ( specify exactly what you want if you need more help ) – Arun A S Mar 03 '15 at 07:55
  • Yes, I just updated the code and made some comments in the code. I hope it clears the confusion a little. – nm10563 Mar 03 '15 at 08:17

5 Answers5

1

The code for random number generation is inside the loop that runs 31 times.

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

If you want the random number to be generated only once, remove the for loop.

Ayushi Jha
  • 4,003
  • 3
  • 26
  • 43
0
myVar = rand() % n + 1;

this is inside a for loop which is running 30 times

war1oc
  • 2,705
  • 1
  • 17
  • 20
  • i know but im trying to generate a random number once. My 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. – nm10563 Mar 03 '15 at 07:14
0

what are you trying to do? If you are trying to just get the number printed once just omit the for loop:

#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);
guy
  • 342
  • 2
  • 11
0

Well the problem is you are using a for loop which creates 31 ( since c = 0 ; c <= 30 ) random numbers in the range and prints all 31. If you want to print just 1 random number, just remove the for loop. You actually don't need myVar in this code. You can just directly print the random number without storing it. This is how

#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

}

Also, I do not understand what you mean by using an array, so give more details about what you were thinking.

Arun A S
  • 6,421
  • 4
  • 29
  • 43
0

Use,

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

or if want to generate unique random numbers, use urand() lib which is found in the github.

Karthikeyan
  • 381
  • 8
  • 19