home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 8 / CDASC08.ISO / NEWS / 554 / JUIN / RANDOM.PAS < prev    next >
Pascal/Delphi Source File  |  1993-10-07  |  2KB  |  60 lines

  1. {─ Fido Pascal Conference ────────────────────────────────────────────── PASCAL ─
  2. Msg  : 333 of 389                                                               
  3. From : Erik Johnson                        1:104/28.0           15 Jun 93  18:43 
  4. To   : Ben Glazer                                                                
  5. Subj : RANDOM NUMBER GENERATOR                                                
  6. ────────────────────────────────────────────────────────────────────────────────
  7. > EJ> The random number generator in Pascal is not very 
  8. > EJ> good.  I have one that
  9. > EJ> takes 3 seeds and will not repeat for some trillion or so numbers.
  10. > EJ> can supply you with the code if you are interested.
  11. >I'd like to see this one, Erik!  Please, post away...
  12.  
  13. Here is the one I started with (as copied out of some long forgotten
  14. programming journal).  Unfortunately it is uncommented, and we are
  15. advised by the original columnist that modying the results can cripple
  16. the 'randomness' unless one knows they are doing.  This produces
  17. something like 'billions' of random numbers w/o repeating.  I have since
  18. modified (object oriented, longints...) but this version is smaller and
  19. simpler.}
  20.  
  21. unit random;
  22. {$D-}
  23.  
  24. interface
  25.  
  26. type
  27.     seedtype=record
  28.       seed1,seed2,seed3:word
  29.     end;
  30.  
  31. function ran(range:word):integer;
  32. procedure getseed(var s1,s2,s3:word);
  33. procedure setseed(s1,s2,s3:word);
  34. procedure incseed(num:longint);
  35.  
  36. var randomseed:seedtype;
  37.  
  38.  
  39. implementation
  40.  
  41. function ran(range:word):integer;
  42. begin
  43.   with randomseed do begin
  44.     seed1:=seed1*179 mod 32771;
  45.     seed2:=seed2*183 mod 32779;
  46.     seed3:=seed3*182 mod 32783
  47.   end;
  48.   ran:=0;
  49.   if range>1 then
  50. ran:=(randomseed.seed1+randomseed.seed2+randomseed.seed3) mod range
  51. end;
  52.  
  53. ** The rest got garbled, but they aren't crucial.  Procs for setting and
  54. getting the 3 current seed values, and incrementing the current seed
  55. values without getting a random number back.
  56.  
  57. I'm not sure how the numerical constants in the RAN function are
  58. derived, but I don't recommend changing them, unless you know how
  59. they're derived.