PDA

View Full Version : Modula-2 and Random Number Generation


Maserk
12-03-2002, 08:58 PM
Hey guys. I am trying to write a simple Library Module for generating random numbers. I am very new to Modula-2 and cannot find decent bit manipulation modules to implement the algorithms I want to use to generate the random numbers. I was wondering if you guys knew of any easy formula's for generating random numbers (in either int, long, double, etc..etc..) that would be easy to implement? Also does anyone know of any decent modula-2 compilers? Thanks guys.

file13
12-04-2002, 01:30 PM
i don't know Modula-2, but i'm very fond of it's big sister Ada.

in Ada you can do random generation something like this (this is a craps jiggly but uses a generic dice rolling function):


-- craps.adb

with Ada.Text_IO;
with Ada.Numerics.Discrete_Random;

procedure Craps is

------------------
-- Dice_Total --
------------------
--
-- A Generic dice rolling function to get a total
-- when given a number of dice and the numeric total
-- of each die.
-- Returns 0 if the sides are invalid (0 or negative).
--
function Dice_Total (Number_Of_Dice : Integer;
Sides : Integer)
return Integer
is
subtype Dice_Range is Integer range 1 .. Sides;
package Roller is new
Ada.Numerics.Discrete_Random (Dice_Range);

Seed : Roller.Generator;
Roll : Dice_Range;
Total_Roll : Integer := 0;
begin
Roller.Reset (Seed);
for I in Integer range 1 .. Number_Of_Dice loop
Roll := Roller.Random (Seed);
Total_Roll := Total_Roll + Roll;
end loop;
return Total_Roll;
exception
when Constraint_Error =>
return 0;
end Dice_Total;

begin
Ada.Text_IO.Put_Line ("Craps Roll:" & Integer'Image (Dice_Total (2, 6)));
end Craps;


if nothing more maybe you'll want to check out Ada as opposed to Modula since Ada is Modula on speed. also, the best Ada compiler is the GNU Ada (called GNAT) and it is absolutly free. plus Ada has a decent sized community which is very active--they just finished a .NET port called #A, not to mention there's support for all the trendy stuff like XML and what not. if you're interested (unless of course you have to learn Modula for school/work) then i'd give Ada a look.

GNAT (GNU Ada 95):
http://libre.act-europe.fr/

A# (Ada for .NET):
http://www.usafa.af.mil/dfcs/bios/mcc_html/a_sharp.html

Adagide (IDE for Windows--i use Xemacs but i love emacs):
http://www.usafa.af.mil/dfcs/bios/mcc_html/adagide.html

Tutorials (including full length free books!):
http://www.adapower.com/learn/

General links:
http://www.adapower.com/ (the BEST)
http://www.qlippoth.com/ada.html (my links)

anyways, good luck with whatever language you choose!

Maserk
12-04-2002, 04:45 PM
Thanks bud.