home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / pascal / 6698 < prev    next >
Encoding:
Text File  |  1992-11-18  |  2.6 KB  |  61 lines

  1. Newsgroups: comp.lang.pascal
  2. Path: sparky!uunet!utcsri!skule.ecf!torn!news.ccs.queensu.ca!mast.queensu.ca!dmurdoch
  3. From: dmurdoch@mast.queensu.ca (Duncan Murdoch)
  4. Subject: Re: Encryption and Random Numbers
  5. Message-ID: <dmurdoch.288.722119961@mast.queensu.ca>
  6. Lines: 49
  7. Sender: news@knot.ccs.queensu.ca (Netnews control)
  8. Organization: Queen's University
  9. References: <1992Nov17.211502.18511@lynx.dac.northeastern.edu> <dmurdoch.285.722096509@mast.queensu.ca>
  10. Date: Wed, 18 Nov 1992 20:52:41 GMT
  11.  
  12. >In article <1992Nov17.211502.18511@lynx.dac.northeastern.edu> cschmidt@lynx.dac.northeastern.edu (Christopher Schmidt) writes:
  13. >>generator.  It's a nice technique, except that if Borland
  14. >>modifies the TP random number generator, then you and your
  15. >>customers will be locked out of your encrypted data.
  16.  
  17. I've just checked in BP 7.  It appears that the Randseed update algorithm is 
  18. the same, but the translation from Randseed to Random(N) has changed.
  19.  
  20. Here's what happens now in a call to Random(N):
  21.  
  22. First, Randseed is updated.  The update formula is
  23.  
  24.   randseed := randseed*134775813 + 1;
  25.  
  26. Then, treating randseed as an unsigned 32 bit quantity, Random(N) returns
  27.  
  28.   Random := trunc(N*randseed/2^32);
  29.  
  30. The calculations inside the parentheses above must *not* be allowed to 
  31. overflow; this makes implementation sort of tricky.  One way to do it is
  32.  
  33.   if Randseed < 0 then
  34.     Random := trunc(N*(randseed + 4294967296.0))/4294967296.0)
  35.   else
  36.     Random := trunc(N*double(randseed)/4294967296.0);
  37.  
  38. I don't have the code handy to simulate the TP 6.0 Random(N) call, but it 
  39. returns the high word of RandSeed mod N. (This may have changed between 
  40. releases of 6.0; there were lots of complaints about the lack of uniformity 
  41. of the distibution.)
  42.  
  43. In the special case of N=256 that I used in my encryption filter, the 
  44. difference is that TP 6 gives you the 2nd most significant byte of Randseed
  45. as Random(256), while BP 7 gives you the most significant byte.
  46.  
  47. Finally, there's a difference between TP 6 and BP 7 in the way the 
  48. floating point Random values are generated.  BP 7 treats Randseed as a
  49. Longint, and returns (RandSeed+2^31)/2^32.  TP 6 also treated Randseed as a 
  50. Longint, but returned Abs(RandSeed)/2^31.  (This was in $N+ mode; in $N- 
  51. mode, TP 6 treated Randseed as unsigned, and returned RandSeed/2^32.)
  52.  
  53. It appears to me both of these changes are good.  There were two bugs 
  54. listed in my TP 6 bug list about random number generation:  first, the non-
  55. uniformity of Random(N) which has been remedied to a great extent, and 
  56. second, the possibility that Random = 1.0, which has now been removed.
  57.  
  58.  
  59. Duncan Murdoch
  60. dmurdoch@mast.queensu.ca
  61.