## generating random numbers, vectors, etc. and random seed

Librairie C++ de calcul formel/ C++ symbolic computation library

jocaps
Messages : 118
Inscription : lun. avr. 17, 2017 4:32 pm

### generating random numbers, vectors, etc. and random seed

Hi,

How do you set the seed of the random number generator for giac. I want to specifically used ranm and rand but I get the impression that I am using randseed wrongly.

Code : Tout sélectionner

``````from giacpy import randseed
randseed(123)
``````
Edit: I realized that there is a bug in python that does not appear in Xcas.

In Xcas, randseed and srand both will return the seed that I give. But in python this is not happening and so I get a different random number even if I have the same seed set. Here is what I get in python:

Code : Tout sélectionner

``````>>> from giacpy import rand, randseed
>>> randseed(12)
1648307537
>>> rand(111)
67
>>> randseed(12)
615922008
>>> rand(111)
41
``````
Here is what happens in XCas:

Code : Tout sélectionner

``````randseed(12)
12
rand(111)
88
randseed(12)
12
rand(111)
88
rand(111)
51
``````

frederic han
Messages : 1113
Inscription : dim. mai 20, 2007 7:09 am
Localisation : Paris
Contact :

### Re: generating random numbers, vectors, etc. and random seed

The problem is that randseed seems special in giac because in giac/xcas:
randseed doesn't need () while other functions without arguments does.

So in giacpy it should have special treatment!

You can fix it in your own code with the current giacpy like this:

Code : Tout sélectionner

``````from giacpy import Pygen, randvector
randseed=Pygen('randseed')  # or any name of your choice if you worry about a later from giacpy import *
>>> randseed(5)
5
>>> randvector(15)
[-28,16,77,-74,28,-47,-20,-49,49,25,-50,67,86,-80,-44]
>>> randseed(5)
5
>>> randvector(15)
[-28,16,77,-74,28,-47,-20,-49,49,25,-50,67,86,-80,-44]
>>>
``````

jocaps
Messages : 118
Inscription : lun. avr. 17, 2017 4:32 pm

### Re: generating random numbers, vectors, etc. and random seed

Thanks Frederic. This worked. Now I can rely on the randomizer.