-1

I am in the middle of making a lottery program and this is my randomization function

#include <iostream>
#include <stdlib.h>
#include <time.h>// for seeding srand
#include <algorithm> //for std::find
#include <iterator> //for std::begin, std::end

using namespace std;

void rivinArvonta(int arvottuRivi[]){   
    int rivi[7];
    int pallo = 41;
    srand(time(NULL));

    for (int i = 0; i < 7; i++) {
            pallo = rand() % 40 + 1;
            bool exists = (find(begin(rivi), end(rivi), pallo) != end(rivi));
            if (exists == false) {
                    rivi[i] = pallo;
            }else
                    i = i - 1;
    }
    sort(rivi, rivi + 7);


    for (int i = 0; i < 7; i++){
        arvottuRivi[i] = rivi[i];
    }
}

The program runs in a while loop until I tell it to stop. The first time I call this function, it returns something like:

 5, 0, -164820691, 21983, 0, 0, -164820768

The second time I call the function it works just as intended.

I don't understand how pallo = rand() % 40 + 1; can return something so much out of the bounds? Could this be because I use g++?

PS sorry for having my variable names in finnish.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
Smig
  • 1
  • 8
    Smells a bit like undefined behaviour: using an uinitialized local varaible (`rivi`). – Adrian Mole Mar 01 '20 at 21:02
  • it's either in C or C++, so please remove one of the tags. Then the rivi array is not initialized, so contains garbage like the -1648... values, and most likely the find doesn't work correctly because of that... – B. Go Mar 01 '20 at 21:02
  • Does this answer your question? [array initialisation](https://stackoverflow.com/questions/1628311/array-initialisation) – Adrian Mole Mar 01 '20 at 21:11
  • For me it is giving intended output every time, please compile and share your code using any online compiler, I want to see how are you calling your function. – Sumit Mar 01 '20 at 21:33
  • 1
    Also you should call `srand` once per program, not once per function call – M.M Mar 01 '20 at 22:38
  • 1
    @Sumit The code has undefined behavior. It might not be possible to reproduce the concrete output OP is getting easily. – walnut Mar 01 '20 at 22:38
  • You also should call `srand` only once at the program start, not inside a loop. If the loop runs in less than one second, you can easily get identical results. – aschepler Mar 01 '20 at 22:41

1 Answers1

-1

rivi is uninitialised data the first time round so you get garbage as Adrian mole suggested. The second call will use the same array in the same memory location after it would have been initialised by the first call, which is why you get sensible results. Also don’t apologise for being lucky enough to speak other languages!

Clifford
  • 88,407
  • 13
  • 85
  • 165
FShrike
  • 323
  • 1
  • 10
  • 2
    *The second call will use the same array in the same memory location* - not necessarily, as it stands! You would need to add the `static` keyword to ensure values are maintained across different calls to the function. – Adrian Mole Mar 01 '20 at 21:07
  • I know this, but that’s my only thought for the correct results second time round - I assume he is calling the functions back to back. – FShrike Mar 01 '20 at 21:08
  • 2
    @user12962917 even then it's pure coincidence. – J. Doe Mar 01 '20 at 21:09
  • 1
    @J.Doe It's not pure coincidence. The language standard says nothing about overlap between local variables in successive calls to same function, but it's very likely that they'll be allocated in the same memory location, and that the contents on a second call will match the last stored value from the first. The initial value is garbage, but for typical implementations it may be *predictable* garbage. Of course this isn't something you should depend on; rather it's a potential source of bugs that are difficult to track down. – Keith Thompson Mar 01 '20 at 22:35
  • @J.Doe your original comment was ambiguous and in the connotations of spoken English it seemed like it was dismissing my original statement. Keith is correct to specify – FShrike Mar 01 '20 at 22:41
  • @user12962917 that's why i deleted my later comment – J. Doe Mar 01 '20 at 22:59