# Random number generation

**URL:** <https://community.freefem.org/t/random-number-generation/1835>\
**Category:** General Discussion\
**Created:** [June 21, 2022, 7:18pm UTC](https://community.freefem.org/t/random-number-generation/1835 "2022-06-21T19:18:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucassfernandez](https://avatars.discourse-cdn.com/v4/letter/l/8dc957/32.png) [@lucassfernandez](https://community.freefem.org/u/lucassfernandez)\
**Post date:** [June 21, 2022, 7:18pm UTC](https://community.freefem.org/t/random-number-generation/1835/1 "2022-06-21T19:18:29Z")

</div>

Hi, folks!

I need to generate some random real values. I know that FreeFEM is equipped with the functions `randreal1`, `randreal2`, `randreal3`, `randres53`, for generating random real numbers between 0 and 1.

[1] Please, try to run the following code several times.

```auto
for(int i = 0; i < 3; i++){
cout << " ranx = " << randreal3() << endl; 
cout << " \n " << endl; 
}

```

It seems there is a pattern when generating the random values. In particular, the numbers appearing on the first run of code are the same as those appearing in the next executions of the code. This is something that shouldn’t happen, isn’t it?

Remark: I have read some insights for solving this issue. It seems I should start the code with `randinit(100*clock());` However, if `100*clock()` is almost the same for multiple code runs, then the values obtained from `randreal3()` are also the same, i.e., it does not return random values.

[2] Suppose I need to generate random numbers between 1 and 2. Is there a routine for this task?

Thanks in advance,  
Lucas.

---

<div class="post-metadata">

**Author:** ![lucassfernandez](https://avatars.discourse-cdn.com/v4/letter/l/8dc957/32.png) [@lucassfernandez](https://community.freefem.org/u/lucassfernandez)\
**Post date:** [June 24, 2022, 7:14pm UTC](https://community.freefem.org/t/random-number-generation/1835/2 "2022-06-24T19:14:29Z")

</div>

Here is a solution by Professor Frédéric Hecht:

The problem is classique see

do:

```auto
load "ffrandom"
srandomdev(); // set a true ramdom seed ..

```
