-2

So I have a program to make random numbers using a vector and technically it s working but every time I run the program it gives me numbers like, 22 22 34 34 34 34 34 or 13 13 30 30 30 30 30. So it gives me different numbers each time but not really. Any help on getting completely random numbers each time.

#include <iostream>
#include <random>
#include <algorithm>
#include <cstdlib>
#include <time.h>
#include <vector>
using namespace std;

void regular();
void bonus();

int main() {
    regular();
    //bonus();
}


void regular() {
    cout << "These are your regular numbers." << endl;
    int i = 0;
    vector<int> regs;
    srand(time(NULL));
    for (i = 0; i < 7; i++) {
        int x = rand() % 39 + 1;
        regs.push_back(x);
        sort(regs.begin(), regs.end());
        cout << regs[i] << " ";
    }
    cout << endl;
}


void bonus() {
    cout << "These are your bonus numbers." << endl;
    int i = 0;
    srand(time(0));
    for (i = 0; i < 7; i++) {
        cout << rand() % 39 + 1 << "";
    }
}
Jaakko
  • 11
  • 3
  • 1
    Don't use `rand()`, use the [`Random Number Distribution`](https://en.cppreference.com/w/cpp/named_req/RandomNumberDistribution) from the standard. For example [`std::uniform_int_distribution`](https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution). – Timo Sep 19 '19 at 10:42
  • 4
    Possible duplicate of [Why is the new random library better than std::rand()?](https://stackoverflow.com/questions/53040940/why-is-the-new-random-library-better-than-stdrand) – L. F. Sep 19 '19 at 10:48
  • [The problem with randomness.](https://dilbert.com/strip/2001-10-25) – user7860670 Sep 19 '19 at 11:39
  • BTW, it seems that in fact you want random permutation(for lottery simulation). [`std::shuffle`](https://en.cppreference.com/w/cpp/algorithm/random_shuffle) is probably what want want so (and then take first numbers). – Jarod42 Sep 19 '19 at 12:00
  • @Timo -- switching generators won't fix this problem. – Pete Becker Sep 19 '19 at 12:14
  • @PeteBecker That's why I made a comment instead of an answer. I just pointed out that using `rand()` in C++ is not a good idea. – Timo Sep 19 '19 at 12:22
  • 1
    @Timo -- despite the rhetoric, `rand()` works perfectly well in many situations. Especially when responding to beginners, be **absolutely clear** that your suggestion is a distraction that **will not** fix their problem. Don't send beginners down a rabbit hole. – Pete Becker Sep 19 '19 at 12:24

1 Answers1

3

The problem is not with rand, but with the fact that you're not printing the number you think you're printing.

You add a number to the vector, then sort the vector, then print the last number of the vector, which isn’t necessarily the number you just added - it’s always the largest one since you just sorted it.

Generate the entire vector first, then sort and print it afterwards and you will see more random randomness.

(And call srand once, in main, so you never do it more than once by accident.)

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • the problem with that is that if I do that and move the sort and cout things out of the for loop I get an error saying Debug Assertion Failed. – Jaakko Sep 19 '19 at 11:27
  • @Jaakko If that happens, you did it wrong and should post another question asking what's wrong with it. – molbdnilo Sep 19 '19 at 11:28
  • Yeah I just realized what was wrong and it is working. Thank you. – Jaakko Sep 19 '19 at 11:30