Task:
There is a dynamic BOMB_COLLECTION array, it has 11 instances of the BOMB class.
There are variables in the class: X and Y (coordinates "bombs"). It is required to make the program itself generate random X and Y coordinates.
I am writing this code:

for (unsigned i = 0; i <=sizeof(BOMB_COLLECTION); i++ )//всего 11 итерац. { int _bombX,_bombY; srand(time(NULL)); _bombX = rand()%301; srand(time(NULL)); _bombY = rand()%601; } 

Result: in all 11 cases, X and Y are the same: (although each time you start it is different.

And if I write:

 for (unsigned i = 0; i <=sizeof(BOMB_COLLECTION); i++ )//всего 11 итерац. { int _bombX,_bombY; _bombX = rand()%301; _bombY = rand()%601; } 

Result: we get the coordinates of the first "bomb" (generated normally), the coordinates of the second "bomb" are also generated normally, but all subsequent bombs copy the coordinates of the SECOND!

  • Using Dev C ++ - chudo116
  • Where: Result: in all 11 cases X and Y are the same: (although when you start, the launch is different. -> all X coordinates are the same, and all Y coordinates are the same, and each time the program is started, these X and Y changes, that is, all -thats something is generated - chudo116
  • one
    1. The result time () in seconds, so inside the loop is the same. 2. Are you sure that _bombX, _bombY are the coordinates of the i-th bomb in the collection , and not just 2 independent variables? - avp
  • 2. I then assign _bombX, _bombY to the i-th instance of the class. I redid the example for clarity. 1. thanks, out of my head! :) - chudo116
  • I support the answer @avp. You simply restart the calculation of the elements of the same sequence ten times. - northerner

2 answers 2

The srand () function initializes the random number generator, and the rand () function generates a random number using this same generator. Therefore, it is necessary to initialize the generator once, and then in the loop each time to get a new random number using rand (). See link text

  • If I understood correctly: // srand (time (NULL)); srand (500); for (unsigned i = 0; i <= sizeof (BOMB_COLLECTION); i ++) {int _bombX, _bombY; _bombX = rand ()% 301; _bombY = rand ()% 601; } But it turns out that in the second case in the task: (copied) we get the coordinates of the first "bomb" (generated normally), the coordinates of the second "bomb" is also generated normally, but all subsequent bombs copy the coordinates of the SECOND! maybe I misunderstood something ( - chudo116
  • In your example, the _bombX and _bombY local, and they do not exist beyond the for() loop. Add these values ​​to BOMB_COLLECTION at each iteration of the loop and then see what values ​​it is filled with. - vladimir_ki

srand (A) generates a sequence of pseudo-random elements depending on its parameter A.

rand () returns the next element from this sequence.

If A is the same with two calls to srand, then we get two identical sequences of pseudo-random elements.

And you need to understand that rand () does not generate, but simply returns the number already generated with srand (A).

  • Thanks for the comment about the essence of the work of rand () - it is. - vladimir_ki
  • 2
    It rand () generates the next pseudo-random number based on the previous one and stores it for the next cycle. srand () sets up the initial element of the sequence, but this setting rand () does not return. - avp
  • @avp all pseudo-random sequence depends on the initial value, and if the first number is known, then all subsequent ones are known. Therefore, rand () only looks like a generating function, but in fact it is not. - IAZ
  • 2
    srand () does not generate any sequence, but establishes a base, on the basis of which rand () on each new call generates the next element of the sequence of pseudo-random numbers. The previous member is stored in a static variable, so rand (), by the way, is not a reentrant function. - skegg
  • 2
    > And you need to understand that rand () does not generate, but simply returns the previously generated number using srand (A). Sorry, but this is not true. The rand () function generates the next value of the pseudo-random sequence. Yes, it is completely determined by the grain. But there is a corresponding area of ​​mathematics and well-established terminology, in which generation means the calculation of the next value based on some (usually recurrent) formula. See the whales: George Marshall, for example. - northerner