
(********************************************************************)
(*                                                                  *)
(*  rand.sd7      Floating point random numbers test                *)
(*  Copyright (C) 1993, 1994, 2004  Thomas Mertes                   *)
(*                                                                  *)
(*  This program is free software; you can redistribute it and/or   *)
(*  modify it under the terms of the GNU General Public License as  *)
(*  published by the Free Software Foundation; either version 2 of  *)
(*  the License, or (at your option) any later version.             *)
(*                                                                  *)
(*  This program is distributed in the hope that it will be useful, *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of  *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   *)
(*  GNU General Public License for more details.                    *)
(*                                                                  *)
(*  You should have received a copy of the GNU General Public       *)
(*  License along with this program; if not, write to the           *)
(*  Free Software Foundation, Inc., 51 Franklin Street,             *)
(*  Fifth Floor, Boston, MA  02110-1301, USA.                       *)
(*                                                                  *)
(********************************************************************)


$ include "seed7_05.s7i";
  include "stdio.s7i";
  include "float.s7i";
  include "keybd.s7i";

const proc: rand_test_1 is func
  local
    var integer: number is 0;
    var integer: count_even is 0;
    var integer: count_odd is 0;
  begin
    while busy_getc(KEYBOARD) = KEY_NONE do
      for number range 1 to 1000 do
        if odd(rand(1, 1000000000)) then
          incr(count_odd);
        else
          incr(count_even);
        end if;
      end for;
      write(count_even <& " " <& count_odd <& " " <& count_odd - count_even <& "  \r");
    end while;
  end func;


const proc: rand_test_2 is func
  local
    var integer: num is 0;
    var integer: counter is 0;
    var integer: number is 0;
    var float: mean is 0.0;
  begin
    while busy_getc(KEYBOARD) = KEY_NONE do
      for num range 1 to 1000 do
        number := rand(0, 1000000);
        incr(counter);
        mean := mean + (flt(number) - mean) / flt(counter);
      end for;
      write(mean <& " " <& counter <& " \r");
      flush(OUT);
    end while;
  end func;


const proc: rand_test_3 is func
  local
    var integer: num is 0;
    var integer: counter is 0;
    var float: number is 0.0;
    var float: sum is 0.0;
    var float: mean is 0.0;
  begin
    while busy_getc(KEYBOARD) = KEY_NONE do
      for num range 1 to 1000 do
        sum +:= rand(-1.0, 1.0);
      end for;
      counter +:= 1000;
      write(sum / flt(counter) digits 28 lpad 31 <& " " <& counter <& " " <& sum <& "    \r");
      flush(OUT);
    end while;
  end func;


const proc: main is func
  local
    var integer: number is 0;
  begin
(*  writeln(rand(0, 2147483647));
    writeln(rand(-2147483646, 2147483647));
    writeln(rand(-2147483647, 2147483647));
    writeln(rand(-2147483647 - 1, 2147483647));
    for number range 1 to 100 do
      while rand(1, 100) <> number do
        noop;
      end while;
      writeln(number);
    end for; *)
(*  writeln(1.0/10000000000000000000000000000.0 digits 28); *)
(*  writeln(10.0);
    writeln(1.0);
    writeln(1.0/2.0);
    writeln(1.0/4.0);
    writeln(1.0/8.0);
    writeln(1.0/16.0);
    writeln(1.0/32.0);
    writeln(1.0/64.0);
    writeln(1.0/128.0);
    writeln(1.0/256.0);
    writeln(1.0/512.0);
    writeln(1.0/1024.0);
    writeln(1.0/2048.0);
    writeln(1.0/4096.0);
    writeln(1.0/8192.0); *)
    writeln;
    rand_test_3;
    writeln;
  end func;
