Hello to everyone who sees this post!

I am not very experienced in programming, but I have some basic knowledge, and the first real programming language I happen to learn is Dev Pascal. I have compiled a program which generates random numbers, using the RANDOM(); function. It is actually a dice roller.

The thing is, that every time I run the program, I get the same sequence of numbers. For example, if I run the program and get 4, 2, 1, 6, the next time I run the program, I will get again 4, 2, 1, 6!

I have searched the Web and found out that this problem is due to the mathematical expression the RANDOM(); function uses, and this makes it "generate" the same numbers every time it runs again, as the "seed", the initial value of the number it uses, is the same. But those ARE NOT random numbers!

If there is anyone who can help me find a better way to generate REALLY random numbers, please, do so! I don't know if there is the possibility to get clock values in Dev Pascal, and if there is, how to use it... Any help will be appreciated. PLEASE HELP ME!!! PLEASE!!! :sad:

Dani AI

Generated

Short summary: the sequence repeated because the pseudo-random generator was using the same seed each run. As discovered (and after pointed to the docs), the usual fix is to seed the generator from the clock once at program start. For quick dice-style randomness use Randomize and then Random(...).

A minimal example:

program DiceRoll;
begin
  Randomize;                // seed PRNG with current time
  writeln('Roll: ', Random(6) + 1);
end.

Practical tips and pitfalls:

  • Call Randomize exactly once at program startup. Calling it repeatedly (especially inside loops) can reduce randomness or even repeat values if the clock resolution is coarse.
  • If you need reproducible runs for debugging or tests, set RandSeed to a fixed integer instead of calling Randomize:
    RandSeed := 42;  // deterministic, repeatable sequence
  • If you still see the same sequence when running the program many times in rapid succession, your environment may seed from a low-resolution clock. In that case either add more entropy (combine time with process id or other values) or use an OS-provided random source.

For cryptographic or security-sensitive randomness do not rely on Random/Randomize. Use the OS CSPRNG (for example, read from /dev/urandom on Unix-like systems or use the Windows Crypto/BCrypt APIs) or a library that exposes cryptographic randomness; then use those bytes directly (or use them to seed a secure PRNG). Avoid inventing your own “better” generator unless you understand cryptographic requirements.

Thank you, Jackrabbit! It really worked!
The error was the missing Randomize; in the beginning. I had the same seed every time, thus getting the same sequence of numbers! Thanks again!
Actually, I found the answer myself, just a few minutes after I posted that... in another website... But the thought and only is worth it! thank you!

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.