samples; coursework: Fixed random seeding with time not working
authorJerome St-Louis <jerome@ecere.com>
Thu, 7 Aug 2014 07:44:48 +0000 (03:44 -0400)
committerJerome St-Louis <jerome@ecere.com>
Thu, 7 Aug 2014 07:44:48 +0000 (03:44 -0400)
- double value would overflow DWORD range on long Windows uptime, and always on Unix as GetTime() returns time since the epoch

12 files changed:
installer/coursework/Chapter 6 - Classes, Methods and Instances/Lab6/lab6.ec
samples/db/MedDB/med.ec
samples/eC/neural/neural.ec
samples/games/cards/poker/poker.ec
samples/games/cards/ruff/src/server.ec
samples/games/cards/tongIts/tongits.ec
samples/games/chess/src/ai.ec
samples/games/chess/src/chess.ec
samples/games/crosswords/CrossWordsServer.ec
samples/games/tetrominoes/tetrominoes.ec
samples/games/ticTacToe/TicTacToe.ec
samples/guiAndGfx/snow/snow.ec

index 268655a..9002aa6 100644 (file)
@@ -576,7 +576,7 @@ class RPGApp : Application
 
    void Main()
    {
-      RandomSeed((uint)(GetTime()*1000));
+      RandomSeed((uint)(((uint64)(GetTime() * 1000)) & MAXDWORD));
       PrintLn("Welcome to this great minimalist RPG!");
       PrintLn("You will need to save the princess from an Evil Sorcerer.");
       PrintLn("But first you should wander the realm to fight the sorcerer's minions, ");
index ca9741a..3e38291 100644 (file)
@@ -154,7 +154,7 @@ class MyApp : GuiApplication
 {
    MyApp()
    {
-      RandomSeed((uint)(GetTime() * 1000));
+      RandomSeed((uint)(((uint64)(GetTime() * 1000)) & MAXDWORD));
       SetDefaultIdField("id");
       SetDefaultNameField("name");
       ds = DataSource { driver = "EDB" };
index 094d460..4651023 100644 (file)
@@ -83,7 +83,7 @@ class NeuralApp : Application
       int i,h,o;
       int c;
 
-      RandomSeed((int)(GetTime() * 1000));
+      RandomSeed((uint)(((uint64)(GetTime() * 1000)) & MAXDWORD));
       // Input to hidden cells synapses
       for(i = 0; i<NUM_HIDDEN; i++)
          hiddenNeurons[i].Init();
index 741ad12..823cf4a 100644 (file)
@@ -454,7 +454,7 @@ class PokerApp : GuiApplication
    {
       int c;
       // Initialize Card Deck
-      RandomSeed((int)(GetTime() * 1000));
+      RandomSeed((uint)(((uint64)(GetTime() * 1000)) & MAXDWORD));
       for(c = 0; c<52; c++)
          deck[c] = c;
 
index 371c129..b1ab834 100644 (file)
@@ -70,7 +70,7 @@ static void Server_ShuffleDeck(Card * deck)
    int t;
    int count;
 
-   RandomSeed((int)(GetTime() * 1000));
+   RandomSeed((uint)(((uint64)(GetTime() * 1000)) & MAXDWORD));
 
    count = GetRandom(1000, 2000);
    for(t = 0; t<count; t++)
@@ -172,7 +172,7 @@ void Server_StartGame(RuffGame game)
    int c;
 
    // New Game
-   RandomSeed((int)(GetTime() * 1000));
+   RandomSeed((uint)(((uint64)(GetTime() * 1000)) & MAXDWORD));
    game.rounds[0].shuffle = (PlayerPosition)GetRandom(0, 3);
    game.round = 0;
    game.gameStarted = true;
index c5cad89..5711302 100644 (file)
@@ -162,7 +162,7 @@ class TongIts : Window
    bool OnCreate()
    {
       int c;
-      RandomSeed((int)(GetTime() * 1000));
+      RandomSeed((uint)(((uint64)(GetTime() * 1000)) & MAXDWORD));
       NewDeckOfCards();
       Shuffle();
       DealCards(numOfPlayers);
index e623d3b..4fc87d9 100644 (file)
@@ -379,7 +379,7 @@ class AIThread : Thread
       int startRating = EvaluateMaterial(chessState->board, chessState->turn);
       int depth = MAXDEPTH;
 
-      RandomSeed((int)(GetTime() * 1000));
+      RandomSeed((uint)(((uint64)(GetTime() * 1000)) & MAXDWORD));
       aiMoveResult = FindMove(chessState, 0, &depth, &aiMove, startRating, null, 0, null, &abortAI);
 
       return 0;
index 63383a8..19bc3f1 100644 (file)
@@ -149,7 +149,7 @@ class Chess : Window
 
             EnableMenus();
 
-            RandomSeed((int)(GetTime() * 10000));
+            RandomSeed((uint)(((uint64)(GetTime() * 1000)) & MAXDWORD));
 
             NewGame();
          }
index b359ae7..253f611 100644 (file)
@@ -1,6 +1,6 @@
 import "crossWords"
 
-int seed;
+uint seed;
 
 // FOR COMMUNICATION //////////////////////////////////////////
 struct PlayedTile
@@ -185,7 +185,7 @@ class CrossWordsGame
             lettersAvailable[l] = lettersCount[language][l];
          }
 
-         seed = (uint)(GetTime() * 1000);
+         seed = (uint)(((uint64)(GetTime() * 1000)) & MAXDWORD);
          //seed = 256131322;
          RandomSeed(seed);
          //Logf("Seeded with %d\n", seed);
index b06b5e7..d0b937a 100644 (file)
@@ -263,7 +263,7 @@ class Tetrominoes : Window
             board[y][x] = 0;
       turn = CLIENT;
       Update(null);
-      RandomSeed((uint)(GetTime() * 1000));
+      RandomSeed((uint)(((uint64)(GetTime() * 1000)) & MAXDWORD));
       nextPiece = GetRandom(0, 6);
       nextAngle = GetRandom(0, 3);
       NewPiece();
index 36ce40c..9a311cb 100644 (file)
@@ -23,7 +23,7 @@ class TicTacToe : Window
 
    TicTacToe()
    {
-      RandomSeed((uint)(GetTime() * 1000));
+      RandomSeed((uint)(((uint64)(GetTime() * 1000)) & MAXDWORD));
    }
 
    TTTSquare FindTicTacToe(TTTSquare state[3][3])
index 8b217e0..9e01c11 100644 (file)
@@ -30,7 +30,7 @@ class Snowing : Window
    int lastLine;
    Bitmap buffer { };
 
-   Snowing() { RandomSeed((uint)(GetTime() * 100000)); ((GuiApplication)__thisModule).timerResolution = 60; }
+   Snowing() { RandomSeed((uint)(((uint64)(GetTime() * 1000)) & MAXDWORD)); ((GuiApplication)__thisModule).timerResolution = 60; }
 
    Timer timer
    {