Home c++ RAND function in cycle

RAND function in cycle

Author

Date

Category

The function above causing a cycle, you need to make 1600 * 50,000 scrolls.
All are familiar with the time () function, the cycle runs in a second many times more steps than 1. As a result, I get the documents of the same numbers.

To solve my task, I need to receive a random number (probability) of some event.


Answer 1, Authority 100%

SRAND (Time (0)); you need to call once in the beginning. Accident will be normal.
Permanent call SRAND Accident does not improve.

Supplement

If it is very strong to simplify these two functions (very much), the code will look somewhere like this:

int seed = 0; // This is the initial value of the generator
INT GEN = 0; // initial random
int magick = 1234567; // This is such a special magic constant, I came up with it :)
   // In general, there are whole research that define the right constants
   // the same whip can be read
Void Srand (int s) {
 seed = s;
}
Int Rand () {
 RETURN FUNC (GEN, SEED);
}
INT FUNC (Int G, Int S) {
 // This feature based on the previous value calculates the new one.
 // The main thing is that it is necessary to understand that with the same values ​​of the arguments,
 // The result will be the same.
 // The implementation below is one with possible options.
 Return (S * Magick + G)% 0xFFFFFFFF;
}

The question arises – why is it done? Everything is very simple. Make a fast and high-quality generator is quite difficult. Therefore, take the simplest options. It makes no sense to complicate such generators – their quality will not improve this, at best, remain the same. But it is usually only worsening.

Another reason for which this is done is the convenience of debugging. After all, Seed defines the entire sequence. And if you set it the same, then Rand will issue the same sequences.

If you need a random sequence, then there are the following options:

  • buy a special device that will generate random numbers. For example, this is USB-tokenes “Ibank 2 Key” . The first thing that was found in Google is 🙂
  • Collect such a device yourself – Article on Habré . There are many interesting things.
  • Buy a special file with random numbers (yes, this is sold. I understand that many Linux lovers will say, they say I am with / dev / random Help himself, but companies promise good quality.

Answer 2, Authority 33%

This is the so-called. “Frequently arising error.”

If the program code contains multiple attempts to “restart” the pseudo-random number generator, using the current time value received via the Time (NULL)

...
SRAND (TIME (NULL));
...

It may turn out that such calls SRAND will follow each other very quickly. So quickly that Time (NULL) will return the same time value. This will lead to the fact that the pseudo-random number generator will be constantly restarted indicating the same SEED value. As a result, the subsequent calls Rand () will generate the same pseudo-random sequence.

For example, this code, being launched for execution, most likely will withdraw the same pseudo-random sequence twice

# include & lt; stdlib.h & gt;
#Include & lt; stdio.h & gt;
#Include & lt; Time.h & gt;
INT MAIN ()
{
  SRAND (TIME (NULL));
  For (unsigned n = 20; N & gt; 0; --n)
   Printf ("% d", Rand ());
  PrintF ("\ n");
  SRAND (TIME (NULL));
  For (unsigned n = 20; N & gt; 0; --n)
   Printf ("% d", Rand ());
  PrintF ("\ n");
}

This problem is also sometimes reported in the following form: “Under the debuggerrome, I have different random numbers in the program, and without debagger – the same”. Indeed, when the program is performed on steps in an interactive debagger, it is performed “slowly” and between the SRAND Time (NULL) time (NULL) time is time to change Works “How conceived.” But it is worth running a program in free execution, as the problem is manifested in all its glory.

If you need a call SRANDE (Time (NULL)) in your program, then in most cases it is enough to make only one and only once on the start of the program.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions