<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/106221>106221</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [flang][runtime] random_init collision rate is high (>1%) on some platforms and time dependent
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            enhancement,
            flang:runtime
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          jeanPerier
      </td>
    </tr>
</table>

<pre>
    Two calls to random_init close to one another may init random to the same seed with a high probability (>20%) on certain platform at a given time (the collision probability seems to cycle with the time in periods of about 1 minute). This likely shows a lack or randomness in f18 random_init implementation.

This was initially caught by gfortran random_init_2.f90 test in LLVM test suite that sporadically failed: https://github.com/llvm/llvm-test-suite/blob/f6d8e5692b65387703a687a556c2f77111cbfaed/Fortran/gfortran/regression/random_init_2.f90

I went on and made a sampling program 
[sampling.f90](https://github.com/user-attachments/files/16762635/sampling.f90.txt)
 to check random_init collisions between two random_init  calls next to one another.

Attached is a graph that shows the collision rate (in %) in function of the runtime (in seconds).

![f18_random_init](https://github.com/user-attachments/assets/43f98d88-b225-44ab-8723-574f00dd1784)

f18 random_init clearly has a cyclic behavior with spikes where the collision rate goes above 20% for 20 seconds every 2 minutes or so, while it can be zero (no collisions out of 100000 runs) for periods of 20 seconds too. Attached is gnuplot graph and the program to sample it.

The same graph produced by a gfortran compiled exec on the same is mostly flat and never goes above 0.005% collision rate (most samples are 0., with sporadic 4.99999989E-03% samples). Interestingly enough, gfortran is 40x slower in executing random intrinsic calls.

![gfortran_random_init](https://github.com/user-attachments/assets/33aa8210-8c9f-4a4e-98be-b1d902fa45c0)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVkuP2zYQ_jX0ZWBDot4HH3azMRAgBXoIeg1G1EhiliIFklqv--uLkeyNdxsURVEf9CLnwW---cYYgh4s0VEUj6J42uESR-ePPw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