Random number problem

I'm having some trouble getting random numbers for my code


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <ctime>
#include <iostream>
#include <cstdlib>

using namespace std;

int main (){
    srand((unsigned)time(0));
    int number;
    
    number = (rand() % 100) + 1;
    cout << number << endl;
    
    return 0;
}


It will give a random number for the first compile, but after I continue to compile it again, it will just give me increasing values from the first number. i.e. 5 then 7 then 9 then 10, and so on.
I'm just asking if there's a way to fix this without having to use a loop or separate function.
Thank you!
Last edited on
Hello NateB33,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



I ran your code and I believe I could not duplicate what you are saying. Each time I changed something and recompiled the code the numbers appeared to be what they should. Each time I just ran the program the numbers still appeared to be what they should.

You may need to explain what you are doing better. When you say "compile" do you mean that something has changed each time before the program is run. or do you just mean "run", (without a compile)?

I do have 1 suggestion: srand(static_cast<unsigned int>(time(nullptr)));.

Andy
Thank you for the advice Andy! Also, I meant that every time I ran the code, it would seemingly increase the value of the first number it gave.

Here's the stuff from my cmd:

C:\Users\natha\OneDrive\Desktop>g++ -o test test.cpp

C:\Users\natha\OneDrive\Desktop>test
1

C:\Users\natha\OneDrive\Desktop>test
8

C:\Users\natha\OneDrive\Desktop>test
11

C:\Users\natha\OneDrive\Desktop>test
11

C:\Users\natha\OneDrive\Desktop>test
14

C:\Users\natha\OneDrive\Desktop>test
17

C:\Users\natha\OneDrive\Desktop>test
17

C:\Users\natha\OneDrive\Desktop>test
17

C:\Users\natha\OneDrive\Desktop>

I hope this helps. It does it every time I run it. It'll give a random number then increase it for the next run.
It almost seems as if the code you are actually running looks like this:

 
number = (time(0) % 100) + 1;

Hello NateB33,

I can see dutch's point and it makes sense if there is not much time that passes between runs.

Running the program from my IDE I did get this:

53, 72, 85, 98, 8, 21, 38, 54, 64, 77



Running from the command window I got this:

58, 84, 97, 7, 10, 17, 23, 30, 33, 39, 43, 49, 59, 65



For the line number = (rand() % 100) + 1;. Other than emphasizing what you mean the outer () are not really needed based on operator precedence.
https://en.cppreference.com/w/cpp/language/operator_precedence The "rand() % 100" will be done first and then 1 will be added to the result.

If you would like some more information have a look at:
https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful
It is about a 1/2 hour video, but worth the time.

Andy
Thank you all so much! I think I understand what I was doing wrong with it.
Hello NateB33,

When I changed the code to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>

#include <fstream>

using namespace std;

int main()
{
    std::string outFileName{ "Output.txt" };  // <--- Put File name here.

    std::ofstream outFile(outFileName, std::ios::app);

    if (!outFile)
    {
        std::cout << "\n File " << std::quoted(outFileName) << " did not open" << std::endl;
        //std::cout << "\n File \"" << inFileName << "\" did not open." << std::endl;

        return 1;
    }
    //srand((unsigned)time(0));
    srand(static_cast<unsigned int>(time(nullptr)));

    int number{};

    //number = rand() % 100 + 1;
    number = time(nullptr) % 100 + 1;

    cout << number << '\n';

    outFile << number << ", ";

	    // A fair C++ replacement for "system("pause")". Or a way to pause the program.
    // The next line may not be needed. If you have to press enter to see the prompt it is not needed.
    //std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
    std::cout << "\n\n Press Enter to continue: ";
    std::cin.get();

    return 0;  // <--- Not required, but makes a good break point.
}

Line 29 produced something closer to what you are saying. Switch the comments on lines 28 and 29 for a proper run. Writing to the file was for testing and not required. That code can be removed.

Also it is always a good idea to initialize your variables when they are defined.
Andy
That's exactly what I said.
Anyway, he said it's fixed now, but didn't tell us what the problem was.
Topic archived. No new replies allowed.