-5

I wrote a function to generate one random number and I am unable to compile with gcc. I cannot find an error in my syntax and therefore assume that the error is something that I do not know is illegal.

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

int rand_gen(void);

int main(void) {

    int value = rand_gen(void);
    printf("The random number generated was %d", value);
    return 0;
}

int rand_gen(void) {
    int r, x;
    printf("Please seed the random number generator:   ");
    scanf("%d", &x);
    srand(x);
    return r = rand(x) % 20;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Anthony O
  • 622
  • 7
  • 26
  • 1
    And your compiler doesn't know whats wrong either? – tkausl Sep 09 '16 at 00:03
  • Did you try reading the compiler error message? – interjay Sep 09 '16 at 00:04
  • GCC usually tells me there is a syntax error if there is one... What does gcc tell you? – Grady Player Sep 09 '16 at 00:05
  • Common sense would tell you to show the exact error that your compiler is producing. – kaylum Sep 09 '16 at 00:07
  • 1
    does rand() take one parameter? also you should include "time.h" for srand – Raindrop7 Sep 09 '16 at 00:07
  • 2
    *I am unable to compile* is a useless problem description. Are you unable to compile because a) you don't have gcc installed, b) you don't know the command to use, c) you broke the *g* and *c* keys on your keyboard, d) you get a compiler error that you can't share with us because someone told you error messages are top secret, or e) some other mysterious reason that you can't compile but don't want to explain. Be serious - you have the error message right in front of your eyes on your screen, and there is absolutely no reason for you to fail to include it in your question. – Ken White Sep 09 '16 at 00:10
  • @raindrop7: why would `` be relevant given that `srand()` is declared in ``? It's common to need `` too because many people use `srand(time(0));`, but that's not relevant to this code. – Jonathan Leffler Sep 09 '16 at 00:18
  • yes that is what I wanted to say including time.h for time() thanx – Raindrop7 Sep 09 '16 at 00:20
  • 1
    Please note [`srand()` — why call it only once?](http://stackoverflow.com/questions/7343833/srand-why-call-it-only-once/) Apart from annoying your user if you ask them to seed the random number generator each time your program wants a random number, what you get as random numbers isn't very random any more (the user might type 2 each time you ask). Also, you should check that the `scanf()` accepted a value; you should check whether inputs work always — people have annoying habits like typing `infinity` where you wanted them to type `0`. Separate the seeding from the random number generating. – Jonathan Leffler Sep 09 '16 at 00:24
  • @JonathanLeffler I think the purpose of this program is to allow the user to enter whatever seeds they choose, and see what happens. But who knows. – M.M Sep 09 '16 at 00:28
  • @M.M: As you say, it is hard to know, but the `rand_gen()` function shown isn't usable in a million-iteration loop, for example. – Jonathan Leffler Sep 09 '16 at 00:29
  • @AnthonyOrona: Welcome to Stack Overflow. Please read the [About] and [Ask] pages soon. Your question is on hold because your question doesn't make it clear what is wrong; as another [eloquent diatribe](http://stackoverflow.com/questions/39401859/is-it-legal-to-call-printf-and-scanf-from-within-another-function-random-numbe/39401943?noredirect=1#comment66129796_39401859) notes, you should show us the error message. It's tempting to mark it 'on hold' because of 'a trivial typo' too — the problem is basically a trivial typo. Please read up on how to create an MCVE ([MCVE]) too. It's important. – Jonathan Leffler Sep 09 '16 at 00:33
  • While you _want_ a `void` in your _prototype_ for `rand_gen` (i.e.) `int rand_gen(void);` at the top. You do _not_ want `void` when _calling_ it (i.e.) change `int value = rand_gen(void);` into `int value = rand_gen();` Also, the libc function `rand` does _not_ take an argument, so use `rand()` instead of `rand(x)` – Craig Estey Sep 09 '16 at 00:48

1 Answers1

2

Not great at C, but I see a couple things.

  1. rand() doesn't take an input, its just void.

  2. Don't return a statement like that. Try:

    r = rand() % 20;
    return r;
    

    Something along those lines

  3. I don't think you're using srand() correctly. Here's the documentation

  4. More on a stylistic note, but you should be tabbing (4 spaces) whenever you enter another layer of a function like:

    int main(void) {
        whatever;
        whatever;
        return whatever;
    }
    whatever;
    

Might not fix all your problems but it's a start.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Joey Wood
  • 273
  • 1
  • 10
  • `return r =rand() % 20;` is legal – M.M Sep 09 '16 at 00:15
  • 1
    The use of `srand` is correct – M.M Sep 09 '16 at 00:15
  • 1
    @M.M: Nominally, `srand()` takes an `unsigned int` rather than a (signed) `int`, but other than that, it is correct. And I agree that the `return` statement is legal, albeit that the assignment to a local variable as you leave the function is moderately pointless. Separating the assignment and `return` makes it easier to step through the code with a debugger, though. – Jonathan Leffler Sep 09 '16 at 00:21
  • 1
    @JonathanLeffler There is implicit conversion from `int` to `unsigned int`, which is well-defined. – M.M Sep 09 '16 at 00:27