Random number generator (included): Difference between revisions

Line 261:
=={{header|Factor}}==
The default RNG used when the <code>random</code> vocabulary is used, is the [[wp:Mersenne twister|Mersenne twister]] algorithm [http://docs.factorcode.org/content/article-random.html]. But there are other RNGs available, including [[wp:SFMT|SFMT]], the system RNG ([[wp:/dev/random|/dev/random]] on Unix) and [[wp:Blum Blum Shub|Blum Blum Shub]]. It's also very easy to implement your own RNG and integrate it into the system. [http://docs.factorcode.org/content/article-random-protocol.html]
 
=={{header|Fortran}}==
Fortran has intrinsic random_seed() and random_number() subroutines. Used algorithm of the pseudorandom number generator is compiler dependent (not specified in ISO Fortran Standard, see ISO/IEC 1539-1:2010 (E), 13.7.135 RANDOM NUMBER). For algorithm in GNU gfortran see https://gcc.gnu.org/onlinedocs/gfortran/RANDOM_005fNUMBER.html
Note that with the GNU gfortran compiler program needs to call random_seed with a random SET= argument to get a pseudorandom number otherwise the sequence always starts with the same number. Intel compiler ifort reinitializes the seed randomly without SET argument to random value using the system date and time. Here we are seeding random_seed() with some number obtained from the Linux urandom device.
 
<lang fortran>
program rosetta_random
integer, parameter :: rdp = kind(1.d0)
real(rdp) :: num
integer, allocatable :: seed(:)
integer :: un,n
 
call random_seed(size = n)
allocate(seed(n))
 
! Seed with the OS random number generator
open(newunit=un, file="/dev/urandom", access="stream", &
form="unformatted", action="read", status="old", iostat=istat)
if (istat == 0) then
read(un) seed
close(un)
end if
call random_seed (put=seed)
call random_number(num)
write(*,'(E24.16)') num
end program rosetta_random
</lang>
 
=={{header|Free Pascal}}==
Anonymous user