-1

I am teaching myself programming, and as a challenge I tried making a simple text battle system in c++. I used the function rand() to generate pseudo-random numbers. The problem is, they were the same every time you ran the program. e.g. If num1 was in the first turn 1, on the second 0, then 0, 1, 0, 1, 1, 1 etc, if you closed the program and reopened it, it would always be 1, 0, 0, 1, 0, 1, 1, 1... I then looked up how to measure time. I wanted to take an integer expressing exactly how long it took the player to enter a certain string. I followed the tutorials precisely (except I named the variables differently). It did not work. Can anyone please help me and explain how the syntax of this works? I put together a simple program representing exactly what I did, so that you don't have to go through the long, irrelevant code of the entire battle system. I looked up questions like this but nothing worked yet.

#include <iostream>
#include <chrono>


using namespace std;


int main()
{
    auto time1 = std::chrono::high_resolution_clock::now();
    cout << "enter a character:" << endl;
    char blob;
    cin >> blob;
    auto time2 = std::chrono::high_resolution_clock::now();
    std::chrono::duration<double, std::milli> time = timer2 - timer1;
    cout << time;

    return 0;
}
  • 2
    How is the "random number" part relevant here? – mfnx Jan 08 '20 at 11:31
  • What do you want to know? Ask a precise question (how does the syntaxis of "this" work is not a precise question). – mfnx Jan 08 '20 at 11:31
  • 1
    Your program seems to calculate the time needed for a user to input a character. – mfnx Jan 08 '20 at 11:32
  • 1
    I think you're getting a bit mixed up here. There are many folks who use some time-derived value as a **seed** for the in-built random number generator: see [srand function](http://www.cplusplus.com/reference/cstdlib/srand/) - which prevents the repetition you are seeing. – Adrian Mole Jan 08 '20 at 11:35
  • 2
    can you expand on `it did not work`? `rand()` was presumably not random because you hadn't called `srand` with a time, you'd be better off using [c++11's random](https://en.cppreference.com/w/cpp/numeric/random) over `rand()` anyway – Alan Birtles Jan 08 '20 at 11:37
  • Does this answer your question? [How to generate a random number in C++?](https://stackoverflow.com/questions/13445688/how-to-generate-a-random-number-in-c) – xorover Jan 08 '20 at 12:04

2 Answers2

0

Your code does not work as you expected due to 3 reasons:

  • Typo in variable names: change timer1 and timer2 to time1 and time2 respectively.
  • Use duration_cast instead of duration.
  • Use count() method.

count() returns the number of ticks of the type on which you invoke it.

Here is the finished code:

#include <iostream>
#include <chrono>


using namespace std;

int main()
{
    // meausure time at the begining.
    auto time1 = chrono::high_resolution_clock::now();

    cout << "enter a character:" << endl;
    char blob;

    // wait for user input.
    cin >> blob;

    // meausure time at the ending.
    auto time2 = chrono::high_resolution_clock::now();

    // evaluate and print the difference.
    auto time = chrono::duration_cast<chrono::milliseconds>(time2 - time1);
    cout << time.count();

    return 0;
}

But, as @yaodav suggested, there are other better ways to generate random numbers in C++.

xorover
  • 94
  • 9
  • Are you sure this answers the question? Have you read the title of the question? – mfnx Jan 08 '20 at 11:58
  • @mfnx I wanted to take an integer expressing exactly **how long it took the player to enter a certain string.** I followed the tutorials precisely (except I named the variables differently). It did not work. – xorover Jan 08 '20 at 12:00
  • Surely off-topic, but if we keep providing answers to bad questions, the latter will keep coming. – mfnx Jan 08 '20 at 12:11
  • A `duration_cast` shouldn't be required, it is possible to construct a floating point duration from an itegral one. – Alan Birtles Jan 08 '20 at 14:04
0

if you wont to use rand() function you need first to call srand with "seed" this is an example:

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

int main () {
   int i, n;
   time_t t;

   n = 5;

   /* Intializes random number generator */
   srand((unsigned) time(&t));

   /* Print 5 random numbers from 0 to 50 */
   for( i = 0 ; i < n ; i++ ) {
      printf("%d\n", rand() % 50);
   }

   return(0);
}

but like people wrote in the comment is c style code not CPP this is use with CPP

#include <random>
#include <iostream>

int main()
{
    std::random_device dev;
    std::mt19937 rng(dev());
    std::uniform_int_distribution<std::mt19937::result_type> dist6(1,6); // distribution in range [1, 6]

    std::cout << dist6(rng) << std::endl;
}

How to generate a random number in C++?

yaodav
  • 1,126
  • 12
  • 34