0

I'm using C++ and I'm very new, but one of my latest troubles is using srand. Srand is outputting a gargantuan list of numbers until it moves to the next one. Here's my code so you can understand what I'm talking about

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

using namespace std;

void function2()
{
    int num;
    string none;
    string end1;

    while (num != 69420)
    {

        srand((unsigned) time( NULL));

        num = rand() % 100000 + 1;

        cout << num << "\n";
    }
    if (num != 69420)
    {
        cout << "gg's";
        cin >> end1;
    }
}
int main()
{
    int num;
    string none;

    while (num != 69420)
    {

        srand((unsigned) time( NULL));

        num = rand() % 100000 + 1;

        cout << "Press enter.";
        getline(cin, none);
        if (none == " ")
        {

        }
        else
        {
            function2();
        }
        if (num != 69420)
        {
            break;
        }
        else
        {
            continue;
        }
    }
}

This probably isn't right and that's why I'm here. Thanks to anyone if you try to help me!

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 2
    That is not how to use `srand`. I recommend a read through some [documentation](https://en.cppreference.com/w/c/numeric/random/srand) to learn what it does and how it should be used. – user4581301 Mar 03 '20 at 18:04
  • 2
    You should call `srand` only once per your program: [Recommended way to initialize srand?](https://stackoverflow.com/questions/322938/recommended-way-to-initialize-srand) – Algirdas Preidžius Mar 03 '20 at 18:05
  • 1
    Even if you modify this code to seed the generator once before entering your loop, it looks like you WANT to output a gargantuan list of numbers until you randomly hit the desired one? – JohnFilleau Mar 03 '20 at 18:06
  • 2
    `rand` only generates numbers up to `RAND_MAX`, and `RAND_MAX` which could be as low as 32767. You could be stuck generating forever and never generate 69420. – user4581301 Mar 03 '20 at 18:09
  • What resource are you learning C++ from? If you don't currently have a good book, we have a list. https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – JohnFilleau Mar 03 '20 at 18:10
  • 1
    Side note: `rand` was designed to generate random numbers under 1970s constraints like having half a K of RAM and a CPU clocked in the KHz. These days it's not a very good solution. [Fortunately many of its problems have been addressed by additions to the C++ Standard Library](https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful). – user4581301 Mar 03 '20 at 18:15
  • 1
    What do you want your program to do? If your program were a black box (a physical black box that sat on a table), and it had an input and an output, how would you describe the relationship between the two? "It takes input in the form of... and it outputs..." – JohnFilleau Mar 03 '20 at 18:15

0 Answers0