Generating Random numbers and printing "Done!" if they fall within a range in C

Permanent Waves

Sorry if this question has been answered elsewhere, I did search but couldn't find what I was looking for.

Anyway, I am stuck on a university homework problem, the problem asks for me to create a script that randomly generates numbers between 0-99 and prints the number on a new line each time, and if the number just printed falls within the range 68-74 it should then print Done! on the next line and exit the script.

I was given a template with most of the code already done for me, it looks as such:

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

int main() {
    srand(time(NULL));
    /**
     * Your solution must start from below this point. No code modifications WHATSOEVER are allowed ABOVE this point!
     */


    /**
     * Your solution must have finished by this point. No code modifications WHATSOEVER are allowed BELOW this point!
     */
    return 0;
}

int getRandInt() {
    return rand() % 100;
}

Anyway after a lot of messing around I finally got it to print a list random numbers rather than 1 random number infinitely. I did this using:

do
    printf ("%d\n", getRandInt());
while (getRandInt());

I tried plugging in things like:

do
    printf ("%d\n", getRandInt());
while (getRandInt() <68);

to see if it would at least only print random integers less than 68, but this just makes it print only 1 or 2 random numbers (sometimes with greater values than 68) instead of the huge list the previous code block prints.

Am I calling the right function? Should I be using a Do While loop? How do I set a range of numbers for the loop to exit and print "Done!"? Obviously I can't edit the code outside of the comments.

I am very new to coding and would appreciated any assistance with this problem.

Sathish

Declare a another integer variable and call the function inside the loop. So every time the return value will be stored in that variable, use that variable for condition checking.

Try the following code snippet-

/**
 * Your solution must start from below this point. No code modifications WHATSOEVER are       allowed ABOVE this point!
 */
    int ret;
    do{
            ret = getRandInt();
            printf("%d\n",ret);

    }while(!(ret >= 68 && ret <= 74));

    printf("Done!\n");
/**
 * Your solution must have finished by this point. No code modifications WHATSOEVER are allowed BELOW this point!
 */

From the above code, when the number falls between 68 to 74 it will print Done! and exit the loop. If not (!(ret >= 68 && ret <= 74)) it will continue and execute the loop till a number falls between 68 to 74.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Generating uniformly distributed random numbers within a range in C++

From Dev

Generating random evenly divisible numbers within a specific range in C++?

From Dev

Generating and storing unique and random numbers within a range

From Dev

Generating random numbers in a particular range

From Dev

C++11 Generating random numbers from frequently changing range

From Dev

Generating random dates within a given range in pandas

From Dev

Generating random dates within a given range in pandas

From Dev

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

From Dev

Generating decimal random numbers in Java in a specific range?

From Java

Generating random whole numbers in JavaScript in a specific range?

From Dev

Generating random numbers over a range in Go

From Dev

Generating small range of different random numbers?

From Dev

Generating random numbers in specific binary range

From Dev

C program for generating random numbers

From Java

Generate 'n' unique random numbers within a range

From Dev

Random numbers within a range excluding zero

From Dev

How to add two random numbers within a range?

From Dev

C# generating random double number range

From Dev

Efficient way of generating random integers within a range in Julia

From Dev

PHP - Generating random integers within specified range from a key

From Java

Generating random numbers in Objective-C

From Dev

C++ generating random numbers inside loop

From Dev

Generating 2 Random Numbers that are Different C#

From Dev

Generating random numbers without repeating.C#

From Dev

C rand() function is not generating random numbers

From Dev

Generating random numbers in c++ language

From Dev

C++ Generating random numbers in functions

From Dev

PHP - Generating random numbers within an interval with respect to the mode X

From Dev

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

Related Related

  1. 1

    Generating uniformly distributed random numbers within a range in C++

  2. 2

    Generating random evenly divisible numbers within a specific range in C++?

  3. 3

    Generating and storing unique and random numbers within a range

  4. 4

    Generating random numbers in a particular range

  5. 5

    C++11 Generating random numbers from frequently changing range

  6. 6

    Generating random dates within a given range in pandas

  7. 7

    Generating random dates within a given range in pandas

  8. 8

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

  9. 9

    Generating decimal random numbers in Java in a specific range?

  10. 10

    Generating random whole numbers in JavaScript in a specific range?

  11. 11

    Generating random numbers over a range in Go

  12. 12

    Generating small range of different random numbers?

  13. 13

    Generating random numbers in specific binary range

  14. 14

    C program for generating random numbers

  15. 15

    Generate 'n' unique random numbers within a range

  16. 16

    Random numbers within a range excluding zero

  17. 17

    How to add two random numbers within a range?

  18. 18

    C# generating random double number range

  19. 19

    Efficient way of generating random integers within a range in Julia

  20. 20

    PHP - Generating random integers within specified range from a key

  21. 21

    Generating random numbers in Objective-C

  22. 22

    C++ generating random numbers inside loop

  23. 23

    Generating 2 Random Numbers that are Different C#

  24. 24

    Generating random numbers without repeating.C#

  25. 25

    C rand() function is not generating random numbers

  26. 26

    Generating random numbers in c++ language

  27. 27

    C++ Generating random numbers in functions

  28. 28

    PHP - Generating random numbers within an interval with respect to the mode X

  29. 29

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

HotTag

Archive