mercredi 8 janvier 2020

Measuring time to generate better random numbers in c++

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;
}



Aucun commentaire:

Enregistrer un commentaire