Random’Numbers’ - UCMnuclear.fis.ucm.es/.../uploads/2011/09/2RandomNumbers.white_.pdf · Whydo...

27
Random Numbers (and pseudorandom numbers)

Transcript of Random’Numbers’ - UCMnuclear.fis.ucm.es/.../uploads/2011/09/2RandomNumbers.white_.pdf · Whydo...

Random  Numbers  

(and  pseudo-­‐random  numbers)  

Why  do  we  need  random  numbers  in  computers?  

•  Cryptography  and  security:  There  is  a  random  number  generator  in  every  computer  kernel  

•  SimulaBon  –  StochasBc  processes:  Brownian  moBon,  Markov  chains,  …  

–  IniBalisaBon  in  MD  –  StochasBc  methods  

•  Monte  Carlo  •  GeneBc  algorithms  •  Simulated  annealing  •  …  

Random  numbers  properBes  

•  The  output  looks  random  (no  structure)  •  If  you  know  one  value  of  the  sequence  you  cannot  predict  the  next…  

•  …  or  the  previous  one.  

•  In  a  sequence  of  random  numbers,  the  probability  distribu7on  for  a  new  number  is  independent  of  all  the  numbers  generated  so  far  

•  Absence  of  correla7ons  

Pseudo-­‐random  Numbers  •  In  computers,  no  truly  random  numbers  so  far  •  Computers  are  determinisBc  •  We  fake  random  numbers  -­‐>  Pseudo-­‐random  numbers  •  We  produce  a  sequence  of  numbers  that  is  difficult  to  disBnguish  from  a  truly  random  number  sequence  

•  These  are  called  pseudo-­‐random  numbers  (random  from  now  on)  

•  The  absence  of  correlaBons  is  a  good  test  of  the  quality  of  the  random  number  generator  

Generator  

π  (1000  digits)  =3.  

•  1415926535  8979323846  2643383279  5028841971  6939937510  5820974944  5923078164  0628620899  8628034825  3421170679  8214808651  3282306647  0938446095  5058223172  5359408128  4811174502  8410270193  8521105559  6446229489  5493038196  4428810975  6659334461  2847564823  3786783165  2712019091  4564856692  3460348610  4543266482  1339360726  0249141273  7245870066  0631558817  4881520920  9628292540  9171536436  7892590360  0113305305  4882046652  1384146951  9415116094  3305727036  5759591953  0921861173  8193261179  3105118548  0744623799  6274956735  1885752724  8912279381  8301194912  9833673362  4406566430  8602139494  6395224737  1907021798  6094370277  0539217176  2931767523  8467481846  7669405132  0005681271  4526356082  7785771342  7577896091  7363717872  1468440901  2249534301  4654958537  1050792279  6892589235  4201995611  2129021960  8640344181  5981362977  4771309960  5187072113  4999999837  2978049951  0597317328  1609631859  5024459455  3469083026  4252230825  3344685035  2619311881  7101000313  7838752886  5875332083  8142061717  7669147303  5982534904  2875546873  1159562863  8823537875  9375195778  1857780532  1712268066  1300192787  6611195909  2164201989  

Working  definiBon  (imprecise)  –  Randomness  of  the  generator  depends  on  the  

applicaBon  •  Randomness  –  The  determinisBc  program  that  produces  a  random  sequence  should  be  different,  and  –  in  all  measurable  respects  –  staBsBcally  uncorrelated  with,  the  computer  program  that  uses  its  output  

–  In  other  words,  any  two  different  random  number  generators  ought  to  produce  staBsBcally  the  same  results  when  coupled  to  your  parBcular  applicaBon  program  

Randomness  is  in  the  eye  of  the  beholder  

•  PragmaBc  point  of  view  •  What  is  random  enough  for  one  applicaBon  may  not  be  random  enough  for  another  

•  SomeBmes  we  need  a  very  good  random  number  generator,  others  we  can  use  a  “dirty”  but  faster  one.  

•  Depends  on  how  many  random  numbers  we  need  

 

TesBng  a  generator  •  A  good  random  number  generator  should  be  able  to  pass  a  set  of  staBsBcal  tests  

•  Test  against  another  random  number  generator  – Minimal  Standard  generator  +  shuffle  

•  DIEHARD  –  hgp://www.stat.fsu.edu/pub/diehard/  –  Set  of  tests  for  a  sequence  produced  by  a  random  number  generator  

– Developed  by  George  Marsaglia,  Florida  State  University  

–  hgp://en.wikipedia.org/wiki/Diehard_tests  

Uniform  deviates  •  Random  numbers  that  lie  within  a  specific  range  (typically  [0,1])  

•  There  are  other    distribuBons  that  are  interesBng:  – Gaussian,  –  Poisson,  

•  but  can  be  obtained  transforming  a  uniform  distribuBon  

•  So  the  essenBal  building  block  for  any  sort  of  stochasBc  modeling  or  Monte  Carlo  is  the  uniform  distribuBon  

System  supplied  random  number  generators  

•  OperaBve  systems  have  random  number  generators  (RNG)  implemented  

•  Usually  you  find  two,    – A  strong  one  (slow)  employed  for  generaBng  keys  on  communicaBons  and  cryptography  

– A  weak  one  (fast)  for  computer  applicaBons  

•  The  last  is  the  one  employed  in  programs      

Example:  Linux  Random  Number  Generator  

•  Entropy  in  a  computer  •  You  can  find  a  paper  on  this  issue  in  the  Class  webpage  

System  supplied  RNGs  •  They  are,  almost  always,  “linear  congruenBal  generators”  

•  They  generate  a  sequence  of  integers  I1,  I2,  I3,  …  between  0  and  m-­‐1  by  the  recurrence  relaBon:  

•  where  m  is  the  modulus,  and  a  and  c  are  posiBve  integers  called  the  mul/plier  and  the  increment,  respecBvely  

•  The  recurrence  will  eventually  repeat  itself  with  period  no  greater  than  m  

•  If  m,  a,  and  c  are  properly  chosen  the  period  will  be  of  maximal  length:  m  

I j+1 = aI j + c (mod m)

Details  •  The  linear  congruenBonal  method  is  very  fast  •  It  has  the  disadvantage  that  it  is  not  free  of  sequenBal  correlaBon  

•  If  k  random  numbers  at  a  Bme  are  used  to  plot  points  in  a  k  dimensional  space  (with  each  coordinate  between  0  and  1)  the  points  will  not  tend  to  “fill  up”  the  k-­‐dimensional  space,  but  rather  will  lie  on  (k-­‐1)  –planes  

•  There  will  be  at  most  about  m1/k  such  planes.  If  m,  a,  and  c  are  not  chosen  carefully  there  will  be  many  fewer  than  that  

 

Example  

•  The  number  m  is  usually  chosen  close  to  the  machine  largest  number  ~232  

•  So,  the  number  of  planes  on  which  triple  points  lie  in  3D  space  is  usually  not  greater  than  the  cube  root  of  232,  about  1600  

Example  

•  a  =12,  c  =0,  m  =143  

•  so  this  sequence  has  length  2!  

I j+1 =12I j (mod143)

I j+2 =122 I j (mod143) =144I j (mod143) = I j

The  infamous  RANDU  

•  Widespread  on  IBM  mainframe  computers  for  many  years  with  a=65539,  m=231  

•  It  was  possible  to  generate  random  plots  with  just  11  planes  

•  And  that  is  not  very  random  

Portable  RNGs  

•  There  is  good  evidence  that  

•  Can  be  as  good  as  any  RNG  with  c≠0  

I j+1 = aI j (mod m)

Minimal  Standard  

•  a  =  75  =  16807  •  m  =  231-­‐1  =  2147483647  

•  It  has  a  large  amount  of  successful  use  •  It  is  not  a  perfect  generator  •  But  it  is  a  good  standard  to  test  against  •  RecommendaBon:  Usually  safe  if  you  use  no  more  than  5%  of  the  size  

•  It  is  very  fast  

Problems  

•  Serial  correlaBons:  •  If  successive  points  (Ii,Ii+1)  are  binned  into  a  two-­‐dimensional  plane  for  i=1…N,  the  resulBng  distribuBon  fails  staBsBcal  tests  when  N>107  

•  Low-­‐order  serial  correlaBons  have  been  very  important  historically  and  are  easy  to  remove  using  shuffle  

•  Minimal  Standard  +  shuffling  passes  every  known  staBsBcal  test  (for  m<108)  

 

Shuffling  the  output  

Quick  and  dirty  generators  

•  SomeBmes  we  do  not  need  the  best  generators  

•  We  need  speed  •  We  do  not  need  a  period  longer  than  104  

•  Example:  to  process  data  from  an  experiment  not  always  in  the  same  order,  we  need  to  randomize  a  bit  the  input  

Non-­‐uniform  

•  How  do  we  get  non-­‐uniform  distribuBons?  – TransformaBon  method  – RejecBon  method  

TransformaBon  method  

RejecBon  method  

Notes  on  random  numbers  

•  You  can  find  Chapter  7  of  W.H.  Press,  S.A.  Teukolsky,  W.T.  Vegerling,  B.P.  Flannery,  Numerical  recipes  in  Fortran/C  in  the  Class  webpage  

Real  randomness  is  coming…  •  Atmospheric  noise:  Random.org    •  All  this  is  essenBally  obsolete  in  the  very  near  future  •  Truly  random  numbers  produced  in  the  computer  through  quantum  opBcs  or  agaching  a  Geiger  counter  to  the  PC:  hgp://www.fourmilab.ch/hotbits/  

•  Currently  in  the  market  but  sBll  expensive  for  personal  computers  (~1000  euros  and  above)  

•  But  not  too  expensive  for  big  machines  •  …  or  gaming  and  logery  …  •  …  or  chryptography  (since  2004)