Article View: comp.lang.eiffel
Article #1096Re: Need a random number generator
From: Mike O'Docherty (Teaching Compan...
Date: Thu, 07 Jan 1993 13:44
Date: Thu, 07 Jan 1993 13:44
187 lines
6728 bytes
6728 bytes
mb108@cs.city.ac.uk (omo ADELAKUN T K) writes:
>Hi folks,
>I need a (pseudo-)random number generator for use in a tester program
>for a queuing system. The Eiffel libraries don't appear to have such
If you're talking about ISE Eiffel 2.3 on SunOS then try the following class.
The man page for lrand48 explains the internals.
Mike.
-----------------8<------------------------------------------
-- A class supplying various random entities.
class RANDOMISER
export
seed, integral, alpha, alpha_string, word
feature
words_length:INTEGER is 25000;
word_count:INTEGER;
words:ARRAY[STRING];
-- where to put the words from /usr/dict/words
Create(a_word_count:INTEGER) is
-- 'a_word_count' states how many words you want 'word' to
-- choose from
do
word_count := a_word_count;
end;
seed(val:INTEGER) is
-- Seed the random sequence. See srand48(1) for an
-- explanation of seeds.
external
srand48(seedval:INTEGER)
language "C";
once
srand48(val);
end; -- 'seed'
word:STRING is
-- Provide a word picked at random from
-- /usr/dict/words.
local
word_file:FILE;
stp, i, j:INTEGER;
do
-- only get the words once per object
if words.Void then
word_file.Create("/usr/dict/words");
word_file.open_read;
stp := words_length div word_count;
words.Create(1, word_count);
-- choose a random start point among the first
-- `stp' words and then read every `stp'th
-- word
from
i := integral(1, stp)
until
i = stp
loop
word_file.next_line;
i := i + 1;
end;
from
i := 1;
until
i > word_count
loop
word_file.readline;
words.put(word_file.laststring.duplicate, i);
if i < word_count then
from
j := 1
until
j = stp
loop
word_file.next_line;
j := j + 1;
end;
end;
i := i + 1;
end;
end;
-- return a random word from the generated collection
Result := words.item(integral(1, word_count)).duplicate;
ensure
good_word: not Result.Void and then Result.count /= 0;
end; -- 'word'
integral(lower, upper:INTEGER):INTEGER is
-- Return a random integer in the range
-- [lower .. upper].
external
lrand48:INTEGER
language "C"
do
Result := (lrand48 mod (upper - lower + 1)) + lower;
end; -- 'integral'
alpha_string(min_length, max_length:INTEGER):STRING is
-- Return a string of random letters (lower or upper
-- case) with length in the range
-- [min_length .. max_length].
local
i, stop_at:INTEGER;
do
from
Result.Create(max_length);
stop_at := integral(min_length, max_length);
i := 1;
until
i = stop_at + 1
loop
Result.extend(alpha);
i := i + 1;
end;
end; -- 'alpha_string'
alpha:CHARACTER is
-- Return a random upper or lower case letter.
do
inspect integral(1, 52)
when 1 then Result := 'a';
when 2 then Result := 'b';
when 3 then Result := 'c';
when 4 then Result := 'd';
when 5 then Result := 'e';
when 6 then Result := 'f';
when 7 then Result := 'g';
when 8 then Result := 'h';
when 9 then Result := 'i';
when 10 then Result := 'j';
when 11 then Result := 'k';
when 12 then Result := 'l';
when 13 then Result := 'm';
when 14 then Result := 'n';
when 15 then Result := 'o';
when 16 then Result := 'p';
when 17 then Result := 'q';
when 18 then Result := 'r';
when 19 then Result := 's';
when 20 then Result := 't';
when 21 then Result := 'u';
when 22 then Result := 'v';
when 23 then Result := 'w';
when 24 then Result := 'x';
when 25 then Result := 'y';
when 26 then Result := 'z';
when 27 then Result := 'A';
when 28 then Result := 'B';
when 29 then Result := 'C';
when 30 then Result := 'D';
when 31 then Result := 'E';
when 32 then Result := 'F';
when 33 then Result := 'G';
when 34 then Result := 'H';
when 35 then Result := 'I';
when 36 then Result := 'J';
when 37 then Result := 'K';
when 38 then Result := 'L';
when 39 then Result := 'M';
when 40 then Result := 'N';
when 41 then Result := 'O';
when 42 then Result := 'P';
when 43 then Result := 'Q';
when 44 then Result := 'R';
when 45 then Result := 'S';
when 46 then Result := 'T';
when 47 then Result := 'U';
when 48 then Result := 'V';
when 49 then Result := 'W';
when 50 then Result := 'X';
when 51 then Result := 'Y';
when 52 then Result := 'Z';
end;
end; -- 'alpha'
end -- !class! /RANDOMISER/
Technical Details
Message-ID:
<mod.726414289@cs.man.ac.uk>Path:
arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!agate!doc.ic.ac.uk!uknet!mucs!cs.man.ac.uk!modReferences:
<1993Jan7.082202.7901@city.cs>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!agate!doc.ic.ac.uk!uknet!mucs!cs.man.ac.uk!mod From: mod@cs.man.ac.uk (Mike O'Docherty (Teaching Company Associate)) Newsgroups: comp.lang.eiffel Subject: Re: Need a random number generator Message-ID: <mod.726414289@cs.man.ac.uk> Date: 7 Jan 93 13:44:49 GMT References: <1993Jan7.082202.7901@city.cs> Sender: news@cs.man.ac.uk Lines: 187