-2

I have 2 functions that generate random numbers (first function generates 5 random sets, second function only one. I called srand(time(NULL)) in the first one, and by doing so I didn't have to call it again in the second function.

Problem is, for the second function I keep getting zero (first function is fine). Here are the two functions (these are snippets of the entire code, too long to post it all, unless you guys would need it let me know).

Edit: I #included < ctime > if you're curious, so that's not the issue.

 int Winning_Numbers(int generated[])
 {
     int amount = 5;
     int winning_ticket = generated[amount];

     srand(time(NULL));

     for(int x = 0; x < amount; x++)
     {
         generated[x] = (rand() % 69) + 1;

         while (generated[x] < 1 || generated[x] > 69)
         {
             generated[x] = (rand() % 10) + 1;
         }

         if (x > 0)
         {
             for(int check_number = 0; check_number < x; check_number++)
             {
                 while (generated[x] == generated[check_number])
                 {
                     generated[x] = (rand() % 10) + 1;
                 }
             }
         }
     }

     return winning_ticket;
 }

 int Powerball(int powerball_generated)
 {
     powerball_generated = (rand() % 26) + 1;

     if (powerball_generated < 1 || powerball_generated > 26)
     {
         powerball_generated = (rand() % 10) + 1;
     }

     return powerball_generated;
 }
n0de
  • 155
  • 4
  • 14
  • where is the call to srand? – Pooya Mar 06 '16 at 09:32
  • 6th line. first function ^^ – n0de Mar 06 '16 at 09:33
  • How are you calling the functions? – Nard Mar 06 '16 at 09:34
  • @Nard in my int main(). Here is what I have so far: http://pastebin.com/fZR7UQ1e – n0de Mar 06 '16 at 09:37
  • @Nard Thank you. I tried it and just added both, but the console crashed. Even when I do something like int j = powerball_generated and then return j it still gives out 0. Maybe I'm wrong. – n0de Mar 06 '16 at 09:51
  • You calculated `powerball_generated` but did not assign it to any variable. Do `powerball_number = Powerball(powerball_number)`. Also, don't leave `powerball_number` uninitialized. Do `powerball_number= 0`. – Nard Mar 06 '16 at 09:54
  • @Nard Thanks for the edit. Tried that as well but no luck. Console crashed again lol. – n0de Mar 06 '16 at 10:02
  • @Nard is this what you have? - http://pastebin.com/vf77y7Zf – n0de Mar 06 '16 at 10:03
  • Try looking at your `switch` statement. – Nard Mar 06 '16 at 10:05
  • @Nard I'm missing something obvious and simple I'm assuming lol, I can't figure it out. – n0de Mar 06 '16 at 10:13
  • I notice that your `Powerball()` function both accepts an argument `powerball_generated` and returns it. If you are calling `Powerball(x)` and expecting `x` to be the Powerball number, that's your problem. Try `x = Powerball()` instead. – Steve Summit Mar 06 '16 at 12:13
  • @πάντα ῥεῖ: The linked question has nothing to do with this poster's problem; I don't believe it's a duplicate. – Steve Summit Mar 06 '16 at 12:15
  • @SteveSummit Yeah, it turned out to be something simple and trivial. The question is off-topic anyway it seems. – πάντα ῥεῖ Mar 06 '16 at 12:17
  • @πάντα ῥεῖ: Many beginner questions are difficult for the beginner but seem simple or trivial to an expert. But how does that make the question off-topic? – Steve Summit Mar 06 '16 at 12:20
  • You declared `int rand_numbers[5];` and passed it as `generated` to `Winning_Numbers()`, so you must not access `generated[amount]`, which is `generated[5]`. – MikeCAT Mar 06 '16 at 12:22
  • The function `Chosen_Numbers()` has the same issue. Do not access out-of-range. – MikeCAT Mar 06 '16 at 12:23
  • What type is **generated[]** ? – Arif Burhan Mar 06 '16 at 12:25
  • @SteveSummit How are you considering that question on-topic at all? It's incomplete and unclear, and the real problem doesn't even match the title. Sounds such useful for future research (expert or not)? – πάντα ῥεῖ Mar 06 '16 at 12:44
  • @πάνταῥεῖ When someone doesn't understand why their program isn't working, they often don't understand why their program isn't working, so their question titles often fail to perfectly reflect their actual problem. So it can take a bit of digging on an answerer's part to figure out the actual problem. I keep forgetting that on Stackoverflow we're not even supposed to try in cases like this, that we're supposed to care more about high-quality, referenceable answers than on actually helping the OP. – Steve Summit Mar 06 '16 at 12:58
  • @SteveSummit Thank you for the help, but why would you not try? I'm just learning as a beginner, so when you give me advice on how to make better posts I'll take it into consideration. I just don't understand why you shouldn't bother. Either way I appreciate your help. Thank you – n0de Mar 06 '16 at 19:58
  • @πάνταῥεῖ Because I'm learning. Thanks – n0de Mar 06 '16 at 19:59

1 Answers1

1

Your code has lots of little problems, but the one you're asking about is that in your main function you have

int powerball_number;
Powerball(powerball_number);

Here you are passing the value of powerball_number to the Powerball() function as an argument. But what you want to do is have Powerball() return a value and assign that to the powerball_number variable. So you should write

powerball_number = Powerball();

instead. Also change the Powerball() function to just

int Powerball()

since it doesn't need to take an argument at all.

As a side note, it's much easier for answerers if you can show a small but complete piece of code, that shows your problem, right in your question. Here, the key to your problem showed only in the pastebin fragment, the link to which was hiding in one of the comments, and which it took me a while to find..

Steve Summit
  • 45,437
  • 7
  • 70
  • 103