Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

[solved] numpy.random state seems to repeat across multiple OS runs

edited March 2015 in OpenSesame

Hey there,

I have an experiment that makes use of numpy arrays on various occasions.
Since numpy has a random module, I decided to use that for randomization.

However, on repeated runs the randomization outcome of the numpy functions is repeated.
I have created a (gist)[https://gist.github.com/wkbouter/0614abcf1491fc67f7ba] that implements a minimal example of this problem; the critical item in that example is the script that runs:

import random
import numpy as np

l = [0,1,2,3,4,5,6]
np.random.shuffle(l)
print l
####
l = [0,1,2,3,4,5,6]
random.shuffle(l)
print l

#####
print np.random.random_sample() 
print random.random()

When I hit any of the run buttons, the print outputs are always the same for the np.random module. Oddly though, not for the random module.

>>> 
Starting experiment as ExperimentProcess-11
Expyriment 0.7.0 (Revision 55a4e7e; Python 2.7.8) 
Warning: OpenGL does not support window mode. OpenGL will be deactivated!
openexp.sampler._legacy.init_sound(): sampling freq = 48000, buffer size = 1024
openexp.sampler._legacy.init_sound(): mixer already initialized, closing
experiment.init_log(): using '//*************/quickrun.csv' as logfile (utf-8)
experiment.run(): experiment started at Fri Mar 27 15:15:30 2015
[2, 3, 5, 4, 6, 0, 1]
[0, 5, 6, 1, 3, 2, 4]
0.612798575407
0.113753964223
experiment.run(): experiment finished at Fri Mar 27 15:15:32 2015
>>> 
Starting experiment as ExperimentProcess-12
Expyriment 0.7.0 (Revision 55a4e7e; Python 2.7.8) 
Warning: OpenGL does not support window mode. OpenGL will be deactivated!
openexp.sampler._legacy.init_sound(): sampling freq = 48000, buffer size = 1024
openexp.sampler._legacy.init_sound(): mixer already initialized, closing
experiment.init_log(): using '/*************/defaultlog.csv' as logfile (utf-8)
experiment.run(): experiment started at Fri Mar 27 15:15:37 2015
[2, 3, 5, 4, 6, 0, 1]
[6, 1, 4, 3, 5, 0, 2]
0.612798575407
0.563259958776
experiment.run(): experiment finished at Fri Mar 27 15:15:39 2015
>>> 
Starting experiment as ExperimentProcess-13
Expyriment 0.7.0 (Revision 55a4e7e; Python 2.7.8) 
openexp.sampler._legacy.init_sound(): sampling freq = 48000, buffer size = 1024
openexp.sampler._legacy.init_sound(): mixer already initialized, closing
experiment.init_log(): using '/*************/defaultlog.csv' as logfile (utf-8)
experiment.run(): experiment started at Fri Mar 27 15:15:46 2015
[2, 3, 5, 4, 6, 0, 1]
[3, 4, 5, 1, 2, 6, 0]
0.612798575407
0.383239057903
experiment.run(): experiment finished at Fri Mar 27 15:15:47 2015
>>> 

When I call np.random.shuffle multiple times within an expt it will give novel outcomes consecutively. So randomization within a session works.

Neverthless, the sequence of calls as a whole will always give the same result overall.....

I'm completely at a loss what could cause this -- any thoughts?

Comments

  • edited March 2015

    By the way -- I found I can resolve the issue in terms of its outcome, simply by calling np.random.seed() at the start of my experiment.

    -- but I have very little understanding as to why/where this is happening, and it seems that it shouldn't happen to begin with

  • edited 6:15AM

    Hi Wouter,

    This is quite bizarre, especially because when you execute your code directly in a Python script the random number generator is reinitialized. In general terms it must be a side-effect of how OpenSesame imports and re-imports numpy, but beyond that I have no idea. The solution would be to always call numpy.random.seed() when the experiment is launched, but only when numpy is available (it shouldn't become a dependency). I filed an issue for it here.

    Thanks for spotting this one!

    Cheers,
    Sebastiaan

  • edited March 2015

    Since the seed seems to be reset to the same value whenever opensesame runs, it would seem that something that is either imported or run at startup sets it.

    But since the process repeats within and not between processes, it can't be a hard-coded seed -- Per's suggestion was that maybe some dependency is using the PID as a seed?
    I can't find any out-of-the-ordinary pieces of code in the opensesame source though when searching either for numpy, np or seed.

    Also, seed repeats even when switching backends in between sessions.

  • edited 6:15AM

    But since the process repeats within and not between processes, it can't be a hard-coded seed -- Per's suggestion was that maybe some dependency is using the PID as a seed?

    According to the documentation, it uses either /udev/random or the current time as a seed. And the process id is different on different runs anyway, at least when using the multiprocess runner, which is when the problem occurs (not with the other runners). So it must be that for some reason the numpy random seed is simply not reinitialized, even when started in a different process.

    For the standard random module this is clearly different--it works as expected.

    I can't find any out-of-the-ordinary pieces of code in the opensesame source though when searching either for numpy, np or seed.

    In principle, OpenSesame doesn't use numpy. That way it remains portable across platforms on which numpy isn't available, which at present is only Android. So you won't find any references to numpy (the synth back-end is an exception--there was little choice there).

    So you could say that it's the users' own responsibility to call numpy.random.seed(). However, that's hardly realistic, and I think it makes sense to add a call to numpy.random.seed() to protect users from this weird behavior (wherever the cause may lie).

Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games