Is there a diffrence between the GetTickCount() and rand() functions ? I read they both are Psuedorandom number generators , I would like to know if there is any diffrence between those two functions , I used rand() once and the output was the same each time the program run , would that change if i use GetTickCount instead ? thankyou very much :)

Dani AI

Generated

Short answer: they are not the same. One is an OS uptime/tick counter, the other is a pseudo‑random number generator. The earlier replies from , and pointed at the key issues (deterministic PRNGs and ticking timers), but there are a few practical gotchas and better options worth calling out.

Quick nitpick on the example in this thread: ’s loop has a stray semicolon after the for(...) which makes the loop body run only once — remove that to print ten values.

Why rand() is often the wrong choice

  • rand() is implementation‑defined (on many CRTs, RAND_MAX is small, e.g. 32767), its quality and thread behavior vary, and it’s not suitable for cryptography or high‑quality simulations.
  • Modern C++ offers <random> (engines like std::mt19937 or std::mt19937_64) and distribution types that are reproducible when you want reproducibility, and much better statistically when you don’t.

Better example (C++11+):

#include <random>
#include <iostream>

std::random_device rd;
std::mt19937_64 eng(rd());                 // good general-purpose engine
std::uniform_int_distribution<int> dist(0, 100);

for (int i = 0; i < 10; ++i) std::cout << dist(eng) << '\n';

Why GetTickCount (or similar ticks) is a poor PRNG seed by itself

  • A tick counter is monotonic and predictable; on Windows the 32‑bit GetTickCount() wraps after 2^32 ms (about 49.7 days). Use GetTickCount64() or a high‑resolution timer for timing, but don’t treat ticks as a strong entropy source. Timer granularity may also be coarse (often ~10–16 ms).

Practical advice

  • For general use: seed a good engine with std::random_device.
  • For cryptography: use the OS crypto API (BCryptGenRandom / CryptGenRandom on Windows, getrandom or /dev/urandom on Unix).
  • For reproducible tests: explicitly choose and document the seed.

This keeps randomness predictable when you need it (debugging/tests) and statistically sound when you don’t.

Recommended Answers

All 5 Replies

The reason rand() generated the same numbers each time is that you have to seed the random number generator

#include <iostream>
#include <ctime>

using namespace std;

int main()
{
   
   int i;
   //seeds the number generator with time
   srand((unsigned int)time(NULL));//you only need to call this once in your program
   for(i = 0; i < 10; i++);
   {
      cout<<rand()<<endl;
   }

   return 0;
}

Adding on, GetTickCount() is also a way to seed the random number generator

srand( GetTickCount() );

thanx for the reply guys , i really appreciate it :)

And GetTickCount() should be something like (depending on the system) the number of 'ticks' since the machine booted. On Windows it it milliseconds since boot, and wraps around every month or two (assuming Windows can stay up that long!).

So it is 'random' in the sense that it will be different, but it is a number starting at 1 and growing every millisecond. And if you sample it twice quickly it will be the exact same value.

rand is defined as always returning the same sequence given the same seed so that you can test out your program. That is, every time your program runs it will get the same set of 'random' values so you can fix bugs and the like. Then you add srand() to give it a different seed (with time() or ticks or sampling the time deltas between keystrokes or whatever).

thanx chainsaw

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.