Subversion Repositories Kolibri OS

Rev

Rev 5575 | Rev 5582 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*******************************************************************************
  2.  
  3.     MenuetOS MineSweeper
  4.     Copyright (C) 2003  Ivan Poddubny
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  
  20. *******************************************************************************/
  21. #define MASK_RAND 123456789
  22. #define IQ_RAND 12773
  23. #define IA_RAND 16807
  24. #define IR_RAND 2836
  25. #define IM_RAND 2147483647
  26. #define AM_RAND (1./2147483647)
  27.  
  28. inline fastcall int random( ECX)
  29. // get pseudo-random number - ïîëó÷èòü ïñåâäîñëó÷àéíîå ÷èñëî
  30. {
  31.   $push ebx
  32.  
  33.   $rdtsc        // eax & edx
  34.   $xor eax,edx
  35.   $not eax
  36.  
  37.   EBX = __generator;
  38.   $ror ebx,3
  39.   $xor ebx,0xdeadbeef
  40.   EBX += EAX;
  41.   __generator = EBX;
  42.  
  43.   EAX += EBX;
  44.   EAX = EAX % ECX;
  45.  
  46.   $pop ebx  
  47. }
  48.  
  49. :inline long unirand0(void)
  50. {
  51.          long k,ans,tmp,save;
  52.          save = __generator;
  53.          __generator^=MASK_RAND;   /* avoid __generator==0 */
  54.          k=__generator/IQ_RAND;
  55.          tmp=__generator-k*IQ_RAND;
  56.          __generator*=IA_RAND*tmp;
  57.          __generator-=IR_RAND*k;
  58.          if(__generator<0) __generator+=IM_RAND;
  59.          if(save == __generator) return unirand0();
  60.          ans=__generator;
  61.          __generator^=MASK_RAND;   /* restore unmasked dummy */
  62.          return ans;
  63. }
  64.  
  65. :long RAND_A,RAND_C,RAND_TMP;
  66. :inline long rand(signed long x1,x2)
  67. {
  68.         long tmp,xx;
  69.         RAND_A = __generator;
  70.         __generator = RAND_A*__generator+RAND_C;
  71.         RAND_C = __generator^RAND_A;
  72.         RAND_C>>=1;
  73.         RAND_A<<=1;
  74.         __generator^=RAND_A;
  75.         xx=x2;
  76.         if(x1<0)xx+=-x1;
  77.         tmp = __generator%xx;
  78.         if(tmp<0)tmp=-tmp;
  79.         tmp+=x1;
  80.         return tmp;
  81. }
  82.  
  83. inline fastcall randomize()
  84. // initialize random number __generator - èíèöèàëèçèðîâàòü ãåíåðàòîð ñëó÷àéíûõ ÷èñåë
  85. {
  86.   asm
  87.   {
  88.     mov eax,3
  89.     int 0x40
  90.     ror eax,16
  91.   }
  92.   //if(EAX == __generator)return randomize();
  93.   __generator = EAX;
  94. }
  95.  
  96.