0

The code runs, but the Random Number Generator is only outputting the number 2. I put in the hole code just in case there is any factor I'm not thinking of. Would there be a more convenient way to work the random number generator?

#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
#include <chrono>
#include <thread>

int Time();
int RanNum1to10();

void main() 
{
time_t now = time(0);
tm *ltm = localtime(&now);
int hour = ltm->tm_hour;
int min = 1 + ltm->tm_min;
int sec = 1 + ltm->tm_sec;
int RanNum = RanNum1to10();
std::string text;
std::getline(std::cin, text);
    {
        if(text.find("hi") != std::string::npos)
        {
            RanNum1to10();
            if (RanNum >= 1 && RanNum <= 4)
            {
                Time();
                if(hour >= 0 && hour <= 11)
                {
                    std::cout << "good morning" << std::endl;
                }
                if(hour >= 12 && hour <= 17)
                {
                    std::cout << "good afternoon" << std::endl;
                }
                if(hour >= 18 && hour <= 23)
                {
                    std::cout << "good evening" << std::endl;
                }
            }
            if (RanNum >= 5 && RanNum <= 6)
            {
                {
                    std::cout << "hey" << std::endl;
                }   
            }
            if (RanNum >= 7 && RanNum <= 8)
            {
                {
                    std::cout << "hello" << std::endl;
                }
            }
            if (RanNum >= 9 && RanNum <= 10)
            {
                {
                    std::cout << "hi" << std::endl;
                }
            }
            std::cout << RanNum;
            std::cout << std::endl;
        }
    }
system("pause");
}
int RanNum1to10()
{
    return (rand() % 10 + 1);
}
int Time()
{
    time_t now = time(0);
    tm *ltm = localtime(&now);
    int hour = ltm->tm_hour;
    int min = 1 + ltm->tm_min;
    int sec = 1 + ltm->tm_sec;
    return hour && min && sec;
}
  • 2
    `rand()` is a pretty terrible RNG, but if you have to use it, you need to seed it with `srand()`. And I have absolutely no idea why you have all the extra braces in your code. – T.C. Oct 11 '14 at 15:22
  • 2
    Two is a perfectly random number, so what is the issue? – Ulrich Eckhardt Oct 11 '14 at 15:26
  • @Ulrich Eckhardt: 2 is whats only outputting every time. – Andrew Ryan Oct 11 '14 at 15:28
  • @T.C.: Just used srand() before the RNG and it fixed the problem. Thank you. – Andrew Ryan Oct 11 '14 at 15:29
  • 1
    You know you only set `RanNum` once don't you? When you call `RanNum1to10()` in the loop you don't use the number it produces. – Galik Oct 11 '14 at 15:30
  • @Galik: What number is it using then? I thought RanNum was being ID'd as whatever RanNum1to10 returned. – Andrew Ryan Oct 11 '14 at 15:35
  • @AndrewRyan: I hope you aren't calling `srand()` in your `RanNum1to10()` function. You should only call it _once_ as shown in the second answer to the linked duplicate question. – Blastfurnace Oct 11 '14 at 15:45
  • @AndrewRyan, what Galik said is actually exactly the answer to your problem: There are many calls to RanNum1to10, but only once at the very beginning is the result assigned to RanNum. So no matter how often you call RanNum1to10, you only ever use the result of the first call. – gnasher729 Oct 11 '14 at 20:50

1 Answers1

1

Before you use rand() you need to call

srand(time(NULL));

to initiate the random number generator.

Andy Haskell
  • 677
  • 5
  • 16