Board index » delphi » Random Number Generator

Random Number Generator

I need to make a procedure that will create a random
number between one and 52

Any help would be greatly appreciated, thanx

-Mark Crawshaw (ma...@vision.net.au)

 

Re:Random Number Generator


In article <343bae93.5916...@news.snafu.de> of Wed, 8 Oct 1997 16:20:08
in comp.lang.pascal.borland, Horst Kraemer <horst.krae...@berlin.snafu.d

Quote
e> wrote:
>Just in case you may want to shuffle a deck of cards: this is the most
>efficient algorithm:

>var
>  deck:array[1..52] of integer;
>  i,j,h:integer;
>begin
>  randomize;
>  for i:=1 to 52 do deck[i]:=i;
>  for i:=52 downto 2 do begin
>    j:=Random(i)+1;
>    h:=deck[i];deck[i]:=deck[j]:=deck[j]:=h
>  end
>end.

(after amending the obvious typo)

That is the most efficient algorithm, but ISTM that it is not *always*
the most efficient implementation of the algorithm in TurboPascal 7.0 :
try the following, which, run with all checks ON, are successively
faster (but not so without checks) :-

const lots = 20000 ;
procedure Swap(var y, z : integer) ;
var x : integer ;
begin x := y ; y := z ; z := x end ;

var
  deck:array[1..52] of integer;
  i,j,h:integer;  
  Q, T: longint ; Tix : longint absolute $40:$6C ;
begin Writeln ;
  randomize;

  T := Tix ;
  for Q := 0 to lots do begin
  for i:=1 to 52 do deck[i]:=i;
  for i:=52 downto 2 do begin
    j:=Random(i)+1;
    h:=deck[i];deck[i]:=deck[j];deck[j]:=h ;
    end ;
  end {Q} ;
  Writeln(Tix-T:5) ;

  T := Tix ;
  for Q := 0 to lots do begin
  for i:=1 to 52 do deck[i]:=i;
  for i:=52 downto 2 do begin
    j:=Random(i)+1;
    if i<>j then begin h:=deck[i];deck[i]:=deck[j];deck[j]:=h end ;
    end ;
  end {Q} ;
  Writeln(Tix-T:5) ;

  T := Tix ;
  for Q := 0 to lots do begin
  for i:=1 to 52 do deck[i]:=i;
  for i:=52 downto 2 do begin
    j:=Random(i)+1;
    Swap(deck[i], deck[j]) ;
    end ;
  end {Q} ;
  Writeln(Tix-T:5) ;

  T := Tix ;
  for Q := 0 to lots do begin
  for i:=1 to 52 do deck[i]:=i;
  for i:=52 downto 2 do begin
    j:=Random(i)+1;
    if i<>j then Swap(deck[i], deck[j]) ;
    end ;
  end {Q} ;
  Writeln(Tix-T:5) ;

  Write(^G) ; Readln ;
end.

The "if i<>j then" *sometimes* saves a swap, which helps; the "swap"
procedure uses less array indexing but has calling overheads, and only
helps if checking is on.

Users of newer PCs will want to increase the value of "Lots".

--
John Stockton, Surrey, UK.    j...@merlyn.demon.co.uk    Turnpike v1.12    MIME.
  Web URL: http://www.merlyn.demon.co.uk/ - FAQqish topics, acronyms and links.
  Correct 4-line sig separator is as above, a line comprising "-- " (SoRFC1036)
  Before a reply, quote with ">" / "> ", known to good news readers (SoRFC1036)

Re:Random Number Generator


In article <Pine.GSO.3.95.971008143905.5241A-100000@ringer> of Wed, 8
Oct 1997 14:41:25 in comp.lang.pascal.borland, Federico Balbi

Quote
<fba...@ringer.cs.utsa.edu> wrote:
>function rand:integer;
>begin
> randomize;
> rand := random(52) + 1;
>end;

I assume that this is untested, as it will give the same answer
throughout every 1/18.2 second interval.  "randomize" should only be
called once, not every time.

Test with, for example,
        begin while not keypressed do write(rand:5) end.

As an addendum to my earlier post to this thread, consider the even
quicker code :-

var a, b : ^integer ;

  for i:=52 downto 2 do begin
    j:=Random(i)+1;
    if i<>j then begin a := @deck[i] ; b := @deck[j] ;
      h := a^ ; a^ := b^ ; b^ := h end ;
    end ;

--
John Stockton, Surrey, UK.    j...@merlyn.demon.co.uk    Turnpike v1.12    MIME.
  Web URL: http://www.merlyn.demon.co.uk/ - FAQqish topics, acronyms and links.
  Correct 4-line sig separator is as above, a line comprising "-- " (SoRFC1036)
  Before a reply, quote with ">" / "> ", known to good news readers (SoRFC1036)

Re:Random Number Generator


In article ,
  "Mark Crawshaw"  wrote:

Quote
> I need to make a procedure that will create a random
> number between one and 52
> Any help would be greatly appreciated, thanx
> -Mark Crawshaw (ma...@vision.net.au)

hi mark,
i'm looking at some old notes and here's a random number
generating function.

var
   a2:integer;

function RanGen; real;
var
   t:real
begin
   t:=(a2*10001+5) mod 17417
   a2:=trunc(t);
   RanGen:=abs(t/17417);
end.

so does this help out?

cya
ed
e.n...@mcione.com

-------------------==== Posted via Deja News ====-----------------------
      http://www.dejanews.com/     Search, Read, Post to Usenet

Re:Random Number Generator


Quote
On Thu, 16 Oct 1997 21:12:02 -0600, e.n...@mcione.com wrote:
>In article ,
>  "Mark Crawshaw"  wrote:

>> I need to make a procedure that will create a random
>> number between one and 52
>> Any help would be greatly appreciated, thanx
>> -Mark Crawshaw (ma...@vision.net.au)

>hi mark,
>i'm looking at some old notes and here's a random number
>generating function.

<snipped irrelevant function that did not address the question>

Use the Randomize function (or equivalent) to seed the random number
generator.

To generate a random number between n and m (inclusive), use the general
solution

        num := Random(m - n + 1) + n;

--
Robert B. Clark
Visit ClarkWehyr Enterprises On-Line at http://www.iquest.net/~rclark/ClarkWehyr.html

[My e-mail address has been ROT-13'ed in order to combat spambots.]

Other Threads