Generate a Random number every 2 seconds? C++ Srand - c++

I am studying C++ in order to make a game and I was able to generate a random number every second using the functions of srand. But I wanted the number to be different every 2 second instead.

Say t is the current time in seconds (time(0)). It is obvious that t changes once per second. Then t/2, because of rounding, changes every two seconds.

Here is a simple way to fix the code.
Put a clock() in an infinite while loop and let the clock count so that when it reaches two seconds, it triggers rand() to generate a new random number. Reset the clock(). Repeat infinitely.
Now the Math behind:
As you already know, delta time is the final time, minus the original time.
dt = t - t0
This delta time, though, is simply the amount of time that passes while in the while loop.
The derivative of a function represents an infinitesimal change in the function with respect to one of its variables. Our deltaTime.
The derivative of a function with respect to the variable is defined as http://mathworld.wolfram.com/Derivative.html
f(x + h) - f(x)
f'(x) = lim -----------------
h->0 h
First you get a time, i.e TimeZero = clock() , for reference.
Then you subtract that time from a new time you just got and devide it by h. h is CLOCKS_PER_SEC. Now delta time is
deltaTime = (clock() - TimeZero) / CLOCKS_PER_SEC;
And when deltaTime > secondsToDelay, you generate a new random number.
Putting all that into code results in this:
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int main(int argc, char *argv[]){
cout << "Generate a new random number every 2 seconds \n\n";
// create a clock and start timer
clock_t TimeZero = clock(); //Start timer
double deltaTime = 0;
double secondsToDelay = 2;
bool exit = false;
// generate random seed using time
srand(time(0));
while(!exit) {
// get delta time in seconds
deltaTime = (clock() - TimeZero) / CLOCKS_PER_SEC;
cout << "\b" << secondsToDelay - deltaTime << "\b";
// compare if delta time is 2 or more seconds
if(deltaTime > secondsToDelay){
cout << " ";
// generate new random number
int i = rand() % 100 + 1;
cout << "\nNew random : " << i << " \n";
//reset the clock timers
deltaTime = clock();
TimeZero = clock();
}
}
return 0;
}

Related

timer to calculate seconds not being implemented properly

I am making a timer to calculate the number of seconds until the user presses 3 however the program doesn't work, and no value is saved in the integer variable 'sec'.
where am i wrong?
I have include windows.h and ctime.h
Here's the code:
void func(){
int sec=0
cout<<"Press 3 to end Timer";
cin>>t;
while(t!=3){
Sleep(1);
sec++;}
if(t==3)
{
cout<<"Timer ended";
}
}
This is because cin >> t is blocking. That is, execution doesn't move to your while-loop until the input is complete.
Something like this would work:
#include <chrono>
// This is just to make the example cleaner.
using namespace chrono;
...
system_clock::time_point startTime = system_clock::now();
cin >> t;
system_clock::time_point endTime = system_clock::now();
milliseconds duration = time_point_cast<milliseconds>(endTime - startTime);
At this point, duration.count() is the number of milliseconds spent waiting for input. You can do some math to turn it into seconds, or you could use seconds instead like this:
seconds duration = time_point_cast<seconds>(endTime - startTime);
but in this case, 2.9 seconds will show up as 2 seconds (I think). So I'd do this to output it:
cout << "Duration: " << (double)duration.count() / 1000.0 << endl;
Or something along those lines. I'm typing this raw, so there might be typos.

Why is clock() returning 1.84467e+13?

I am trying to time a code I've got in C++. I have an inner and an outer loop that I want to time separately, but at the same time. For some reason when I do this one of the instances returns 1.84467e+13 and always this exact number.
Why is this happening?
Here is a minimum working example that replicates the effect on my machine:
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main()
{
long int i, j;
clock_t start, finish, tick, tock;
double a = 0.0;
double adding_time, runtime;
start = clock();
for(i=0; i<10; i++)
{
a=0.0;
tick =clock();
for(j=0; j<10000000; j++)
{
a+=1;
}
tock= clock();
adding_time = (double)(tick - tock)/CLOCKS_PER_SEC;
cout << "Computation time:" << adding_time << endl;
}
finish = clock();
runtime = (double)(finish - start)/CLOCKS_PER_SEC;
cout << "Total computation time:" << runtime << endl;
}
Your clock_t is apparently an unsigned 64-bit type.
You're taking tick - tock, where tock was measured after tick, so if there's any difference between the two at all, it's going to try to produce a negative number--but since it's an unsigned type, that's wrapping around to become something close to the largest number that can be represented in that type.
Obviously, you really want to use tock-tick instead.
let say tic = 2ms and tac is 4ms; so when you do tic-tac(2-4) that will generate a negative number obviously.. even if it given a positive number it wont be the real time. and also, the number it generate (which doesnt appear on my computer) is a big number, so, try to use the manipulator;
#include"iomanip"
cout << fixed << showpoint;
cout << setprecision(2);
it might work..

C++ for loop not waiting to finish?

I'm having some trouble with this code:
for (long long int loop = 0; loop < 50000000; loop++)
{
srand( (unsigned)time(NULL) ); // set the seed
int r = abs(rand()); // stores the random seed for this loop
int res = loop + r - r * r / r; // random operations with the loop number and the random seed.
cout << "Loop number: " << loop << ". " << "Result: ";
cout << res << endl;
}//this was missing
If you run that code, you can see, very clearly in the console, that the output of it is only doing the calculations once every few seconds. What's going on? The number should be completely different for each loop, because it's doing calculations with random numbers. Instead, the number changes every x loops ran, and then it only seems to increase between these times it actually does the math.
Do I need to specify I want the loop to wait until everything is complete before moving on?
Because you're doing srand in the loop with time seed. time()'s granularity is in seconds so until one second has passed it will return the same seed and therefore the same random number. Do srand outside the loop.
The point of seeding the rand function with srand is that the sequence of generated random numbers is different with each program run. You need only one srand in your program.
And by the way, rand() always returns a non-negative number, so abs is useless. Be careful though that r can be 0, and you do divide by r, which potentially has undefined behavior. do r = rand()+1 to be safe.
Your seed is the same for the same second, so the random numbers with that seed will be the same. You could try taking out the srand.
srand( (unsigned)time(NULL) ); // set the seed
for (long long int loop = 0; loop < 50000000; loop++)
{
int r = abs(rand()); // stores the random seed for this loop
int res = loop + r - r * r / r; // random operations with the loop number and the random seed.
cout << "Loop number: " << loop << ". " << "Result: ";
cout << res << endl;
}
Cheers

Job scheduling c++ simulation, need advice/suggestion

I have started creating a c++ program to simulate job scheduling algorithms like FiFo and others. I am far from done but my main problem now is how to create the flow of time in my program.
This is my main code so far:
for (i = 1; i < 10; i++)
{
Time1 = clock();
//this is the alogrithm to generate poisson arrival sequence
do{
k = k + 1;
// Generate a random number between 0 and 1
// return a uniform number in [0,1].
double u = rand() / (double)RAND_MAX;
p = p * u;
}while (p > L);
A[i] = k-1;
Time2 = clock();
DT = Time2 -Time1;
TotalTime=TotalTime + DT;
cout << " Total time " << TotalTime
<< " table :" << A[i]
<< " Arrival Time "
<< TotalTime <<endl ;
My main problem is :
my time measuring units with clock are that the time units that are outputted from clock() function are "weird" numbers. Should I use another function?
Results from 10 iteration
Total time 6.19522e+032 table :28 Arrival Time 6.19522e+032
Total time 6.19522e+032 table :29 Arrival Time 6.19522e+032
Total time 6.19522e+032 table :30 Arrival Time 6.19522e+032
Total time 6.19522e+032 table :31 Arrival Time 6.19522e+032
Total time 6.19522e+032 table :32 Arrival Time 6.19522e+032
Total time 6.19522e+032 table :33 Arrival Time 6.19522e+032
Total time 6.19522e+032 table :34 Arrival Time 6.19522e+032
Total time 6.19522e+032 table :35 Arrival Time 6.19522e+032
Total time 6.19522e+032 table :36 Arrival Time 6.19522e+032
PS: I can provide the rest of the code if you want to run it in your machine.
Job scheduling would be much, much easier to do as a "discrete-event" simulation. Check out this tutorial paper to see how such models can be constructed. The framework in the paper is in Java (and has also been implemented in Ruby), but would be straight-up to port to C++.
So the first point is that clock() returns a clock_t variable so, although I don't think it will really make much of a different, make Time1 and Time2 be of type clock_t. As for the main question and from the comments, looks like you just forgot to initialize TotalTime to zero, which would explain the huge numbers.
If the output is all zero then yes the loop probably is running very quickly but you can also increase the number of decimal places you output by using setprecision. Run the following code to see the difference, cout.setprecision(int value) will determine how many decimal places to show.
#include <iostream>
int main() {
double d = 1.0/3.0;
std::cout.precision(15);
std::cout << d << std::endl;
std::cout.precision(3);
std::cout << d;
return 0;
}

C++ clock() function giving incorrect values

I was trying to program a Timer class (unaware that boost had one), then when that wasn't working, I tried to just output the value of clock(), using this code:
#include <ctime>
#include <iostream>
int main()
{
for(int i = 0; i < 50; ++i)
{
std::cout << std::clock() << " ";
}
return 0;
}
When I run the program, I get a series of 0s. I have a similar experience when using boost thread sleep functions to spread out timing a little longer (although after a few seconds, it jumps from 0 to 10,000 and keeps outputting 10,000).
I'm running Gentoo Linux. Is this a platform thing? A C++ thing? What's going on?
Edit: Strangely the jump to 10000 comes after a number of seconds, not milliseconds. When I was sleeping my thread for a second at a time, it took five or six seconds to get to 10000. However, if I'm understanding correctly. The time the thread spends sleeping doesn't contribute towards the clock() count? (Which would make sense; why would it be executing clock cycles if it's sleeping?)
The clock() return value is specified in microseconds. But typical granularity of whatever low-level system call the clock() implementation uses is much lower. So it seems that on your system the granularity is 10ms. Also note that clock() does NOT measure real time - it measures CPU time used by the program. So the time flows when your program controls the CPU, and it freezes when your program is suspended - sleeping, for example.
std::clock's resolution is unspecified. In most cases, it's resolution is going to be about 10ms. Hence the jump.
Try the following:
#include <ctime>
#include <iostream>
int main()
{
for(int i = 0; i < 50; ++i)
{
for (int j = 0; j < 500; ++j )
{
std::cout << j << " ";
}
std::cout << std::endl;
std::cout << std::clock() << std::endl;
}
std::cout << std::endl;
return 0;
}
On my system, I see the return value of clock() staying at 0 until at some point it jumps to 10000. It stays at 10000 till the end. If I remove the std::cout in the inner loop, the return value of clock() stays at 0 all the way through. Looks like clock() returns values in increments of 10000 only.
If I change the inner loop to compute the square root of j and print the return value of sqrt(), the return value of clock() goes up to 50000, but is still increases in increments of 10000.
on my 64 bit operating system the CLOCKS_PER_SEC speed is 1000.and the values of clock comes in milliseconds. perfect timing will be extracted from the code below.
int main(){
clock_t a,b;
int c,d,e,f;
c=clock();
scanf("%d",&e);
d=clock();
f=(d-c)/CLOCKS_PER_SECOND;
printf("timing is %d seconds",f);/*for 64 bit operating system
CLOCKS_PER_SECOND is 1000*/
}