# Do we have a vectorized random number generator?

**URL:** <https://community.freefem.org/t/do-we-have-a-vectorized-random-number-generator/1779>\
**Category:** General Discussion\
**Created:** [May 27, 2022, 1:12pm UTC](https://community.freefem.org/t/do-we-have-a-vectorized-random-number-generator/1779 "2022-05-27T13:12:01Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jade](https://avatars.discourse-cdn.com/v4/letter/j/a8b319/32.png) [@Jade](https://community.freefem.org/u/Jade)\
**Post date:** [May 27, 2022, 1:12pm UTC](https://community.freefem.org/t/do-we-have-a-vectorized-random-number-generator/1779/1 "2022-05-27T13:12:01Z")

</div>

For my model problem I need a P0 function with Gaussian random values on each triangle. So I use the code:

```auto
fespace Nh(Th, P0);
Nh dW;

for (int j=0; j<Th.nt; ++j) {
   dw[][j] = gslrangaussian(ffrng, 1.0); 
}

```

This is inside other loops and is the most time consuming part of my code (much longer than assembly and solve). For example, generate 15000x100 numbers takes about 2 seconds, while the `randn(15000,100)` command in MATLAB takes 0.02. I look through gsl docs and find no way to generate a random vector/matrix without using loops. Importing data from MATLAB will not help because the I/O will become the new bottleneck.

---

<div class="post-metadata">

**Author:** ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)\
**Post date:** [June 7, 2022, 3:38pm UTC](https://community.freefem.org/t/do-we-have-a-vectorized-random-number-generator/1779/2 "2022-06-07T15:38:14Z")

</div>

No to day but is simple to add this feature.

to speed up you can try

```auto
for [j,dwj:dw[]] dwj = gslrangaussian(ffrng, 1.0); 

```

the code

```auto
load "gsl" 
mesh Th = square(100,100); 
fespace Nh(Th, P0);
Nh dW;
real cpu0 = clock(); 
gslrng ffrng=gslrngtype(1);
for (int j=0; j<Th.nt; ++j) {
   dW[][j] = gslrangaussian(ffrng, 1.0); 
}

cout << clock() - cpu0 << endl; ; 
 cpu0 = clock(); 
 
 for [i,dWi: dW[]]
   dWi = gslrangaussian(ffrng, 1.0); 
 cout << clock() - cpu0 << endl; ; 

```

The result on my lap top Apple M1

```auto
  -- Square mesh : nb vertices =10201 , nb triangles = 20000 , nb boundary edges 400
0.017193
0.000999
times: compile 0.010353s, execution 0.019229s, mpirank:0

```

---

<div class="post-metadata">

**Author:** ![Jade](https://avatars.discourse-cdn.com/v4/letter/j/a8b319/32.png) [@Jade](https://community.freefem.org/u/Jade)\
**Post date:** [June 11, 2022, 6:04am UTC](https://community.freefem.org/t/do-we-have-a-vectorized-random-number-generator/1779/3 "2022-06-11T06:04:42Z")

</div>

Thanks a lot! It is quite interesting that the efficiency gain is lost if the statement

```auto
dWi = gslrangaussian(ffrng, 1.0); 

```

is enclosed in a `{}`. What’s special about the syntax `for [i,dWi: dW[]]`?

A related question is how to fix the seed for the gsl random number generator? I want the results reproducible at different runs and in different machines by other researchers. The `randinit` function has no effect.

---

<div class="post-metadata">

**Author:** ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)\
**Post date:** [June 15, 2022, 12:49pm UTC](https://community.freefem.org/t/do-we-have-a-vectorized-random-number-generator/1779/4 "2022-06-15T12:49:04Z")

</div>

the syntaxe

```auto
for [i,dWi: dW[]] code 

```

Is a faster tool to set array.

---

<div class="post-metadata">

**Author:** ![JitendraNathNaik](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/jitendranathnaik/32/2278_2.png) [@JitendraNathNaik](https://community.freefem.org/u/JitendraNathNaik)\
**Post date:** [April 25, 2024, 9:33am UTC](https://community.freefem.org/t/do-we-have-a-vectorized-random-number-generator/1779/5 "2024-04-25T09:33:58Z")

</div>

I am trying to generate normally distributed random numbers using gslrangaussian(ffrng, 1.0); which is using gsl library(load “gsl”). However the numbers appearing on the first run of code are the same as those appearing in the next executions of the code. How to overcome this?

Remark: I have tried using the command srandomdev(); but its showing this error “srandomdev The Identifier srandomdev does not exist”.

Here is the code

load “gsl”  
srandomdev(); // set a true ramdom seed …  
gslrng ffrng=gslrngtype(1);  
for(int i = 0; i \< 10; i++){  
cout \<\< " ranx = " \<\< gslrangaussian(ffrng, 1.0) \<\< endl;  
cout \<\< " \n " \<\< endl;  
}

Thank you in advance,  
Jitendra

---

<div class="post-metadata">

**Author:** ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)\
**Post date:** [April 25, 2024, 1:17pm UTC](https://community.freefem.org/t/do-we-have-a-vectorized-random-number-generator/1779/6 "2024-04-25T13:17:26Z")

</div>

the code srandomdev is in plugin ffrandom  
see [FreeFem-sources/examples/plugin/ffrandom.edp at master · FreeFem/FreeFem-sources · GitHub](https://github.com/FreeFem/FreeFem-sources/blob/master/examples/plugin/ffrandom.edp)

---

<div class="post-metadata">

**Author:** ![JitendraNathNaik](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/jitendranathnaik/32/2278_2.png) [@JitendraNathNaik](https://community.freefem.org/u/JitendraNathNaik)\
**Post date:** [April 26, 2024, 8:13am UTC](https://community.freefem.org/t/do-we-have-a-vectorized-random-number-generator/1779/7 "2024-04-26T08:13:24Z")

</div>

Dear sir,  
Thank you for your response. I have tried using load “ffrandom” but for each run I’m getting the same normally distributed random numbers. Is there any other way or any code in plugin gsl to generate different sets of normally distributed random numbers in each run of the code?

Here is the code I have tried

```
  load "ffrandom"
  load "gsl"
  srandomdev(); // set a true ramdom seed ..
  gslrng ffrng=gslrngtype(1);
  for(int i = 0; i < 5; i++){
    cout << " ranx = " << gslrangaussian(ffrng, 1.0) << endl;
  }

```

Thank you.

---

<div class="post-metadata">

**Author:** ![CarolinePascal](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/carolinepascal/32/2281_2.png) [@CarolinePascal](https://community.freefem.org/u/CarolinePascal)\
**Post date:** [April 26, 2024, 8:38am UTC](https://community.freefem.org/t/do-we-have-a-vectorized-random-number-generator/1779/8 "2024-04-26T08:38:17Z")

</div>

It seems to me that GSL does not depend on ffrandom features, but on its own. You may change the random generator seed by setting the `GSL_RNG_SEED` environment variable, see GSL documentation for an example : [Random Number Distributions — GSL 2.7 documentation](https://www.gnu.org/software/gsl/doc/html/randist.html)

I’m not sure there is an embedded method to change the seed directly in your FreeFem code though - Let me know if you find any !

Best regards,

Caroline.

---

<div class="post-metadata">

**Author:** ![ilavi](https://avatars.discourse-cdn.com/v4/letter/i/b782af/32.png) [@ilavi](https://community.freefem.org/u/ilavi)\
**Post date:** [March 26, 2025, 2:47pm UTC](https://community.freefem.org/t/do-we-have-a-vectorized-random-number-generator/1779/9 "2025-03-26T14:47:35Z")

</div>

I encountered this issue lately and found a way to set the GSL seed in FreeFem.  
gslrng ffrng=gslrngtype(1); // Set up the random number generator  
gslrngset(ffrng, myseed); // Seed generator with myseed
