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

nm10563

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;
}
Ayushi Jha

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to generate a random number within a range in D

How to generate a random number within a range in substrate?

generate a random number within a range but exclude some in C/C++

How do i generate 2 random numbers, once within the range of 50 and 259, and once within 50 and 159?

Generate random number in C within a range and a specific step

How can I generate a random number within a range but exclude some?

XPages SSJS generate a random number within a range

Generate 'n' unique random number within a range

generate random id number within angular template just once

Using Python how do I generate a random number within a range for each row in Pandas dataframe?

How to generate different random number in a range in Swift?

How to generate a random number in a list from a range

Generate random number within defined range, using loops

Generate random number within specified range without REDUNDANCY in TCL

Javascript: Generate a random number within a range using crypto.getRandomValues

How do I generate a random range of numbers within another range?

C++ - generate random numbers following normal distribution within range

Pyspark - how to generate random numbers within a certain range of a column value?

How to generate a cryptographically secure random integer within a range?

How to generate random Double numbers in Google Sheets within range?

How to generate a population of random numbers within a certain exponentially increasing range

How to generate random numbers so that every number only occurs once?

How to generate a random number from 1 to 100 only once?

How to generate random number in while loop only once?

Generate random number in range with SecRandomCopyBytes

Generate random number in range(java)

Generate random number in range in iOS?

c - Generate a random number outside of a range without a loop

Generate a random number between range (-0.5 , 0.5) in C