# Generating random numbers

**URL:** <https://fortran-lang.discourse.group/t/generating-random-numbers/105>\
**Category:** Help\
**Created:** [June 9, 2020, 9:31am UTC](https://fortran-lang.discourse.group/t/generating-random-numbers/105 "2020-06-09T09:31:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ELNS](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/elns/32/3338_2.png) [@ELNS](https://fortran-lang.discourse.group/u/ELNS)\
**Post date:** [June 9, 2020, 9:31am UTC](https://fortran-lang.discourse.group/t/generating-random-numbers/105/1 "2020-06-09T09:31:58Z")

</div>

Hi all,  
How can i generate random numbers in a specific interval such as [0, 100] in fortran?  
I wan to generate and assign them to an array.

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [June 9, 2020, 9:59am UTC](https://fortran-lang.discourse.group/t/generating-random-numbers/105/2 "2020-06-09T09:59:06Z")

</div>

`CALL RANDOM_NUMBER(my_array)` will do the job, but in the 0\<=r\<1 range. Note that `my_array` should of course be `real`.  
`my_array = 100*my_array` will be in the 0\<=r\<100 range. If you want integers including 100, then multiply by 101 and take the integer part!

See: [https://gcc.gnu.org/onlinedocs/gfortran/RANDOM\_005fNUMBER.html](https://gcc.gnu.org/onlinedocs/gfortran/RANDOM_005fNUMBER.html)

You may also want to change the seed each time you launch your program:  
[https://gcc.gnu.org/onlinedocs/gfortran/RANDOM\_005fSEED.html#RANDOM\_005fSEED](https://gcc.gnu.org/onlinedocs/gfortran/RANDOM_005fSEED.html#RANDOM_005fSEED)  
There is also a Fortran 2018 function, but I have never used it:  
[https://gcc.gnu.org/onlinedocs/gfortran/RANDOM\_005fINIT.html#RANDOM\_005fINIT](https://gcc.gnu.org/onlinedocs/gfortran/RANDOM_005fINIT.html#RANDOM_005fINIT)

Because of course these numbers are not random! They are pseudo-random numbers, generally generated by a mathematical sequence. If you don’t change the seed, the sequence will be the same each time you run your program: but this can be interesting if you modify something in your program and want to test if it still gives exactly the same result.

---

<div class="post-metadata">

**Author:** ![milancurcic](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/milancurcic/32/2_2.png) [@milancurcic](https://fortran-lang.discourse.group/u/milancurcic)\
**Post date:** [June 9, 2020, 2:44pm UTC](https://fortran-lang.discourse.group/t/generating-random-numbers/105/3 "2020-06-09T14:44:27Z")

</div>

`random_number` returns samples from a uniform distribution. Here’s a “naive” implementation of random sampling from a normal distribution:

> <https://github.com/modern-fortran/neural-fortran/blob/master/src/lib/mod_random.f90>

I say “naive” because it appears to work but statistics are not my strong suit.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [June 9, 2020, 3:38pm UTC](https://fortran-lang.discourse.group/t/generating-random-numbers/105/4 "2020-06-09T15:38:26Z")

</div>

See also our open issue [#135](https://github.com/fortran-lang/stdlib/issues/135) in stdlib for implementing random number generators from various distributions such as uniform, normal or gamma.

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [June 10, 2020, 12:03am UTC](https://fortran-lang.discourse.group/t/generating-random-numbers/105/5 "2020-06-10T00:03:03Z")

</div>

I’ve got a few super simple examples in my testing framework you could take a look at. I haven’t looked at them at all from a statistical standpoint so I have no idea what the distributions are. They’ll be approximately whatever the underlying random number generator uses I guess. But if you just need something quick and dirty it’s a decent starting point.

> <https://gitlab.com/everythingfunctional/vegetables/-/blob/master/src/Vegetables_m.f90#L4034>
