Page 1 sur 1

generating random numbers, vectors, etc. and random seed

Publié : mar. déc. 19, 2017 12:52 pm
par jocaps
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

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

Publié : jeu. déc. 21, 2017 10:59 am
par frederic han
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]
>>> 

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

Publié : jeu. déc. 21, 2017 7:07 pm
par jocaps
Thanks Frederic. This worked. Now I can rely on the randomizer.