comp.lang.ada
 help / color / mirror / Atom feed
* what's wrong with random
@ 2002-12-22 18:03 Jan
  2002-12-22 18:38 ` Robert A Duff
  0 siblings, 1 reply; 6+ messages in thread
From: Jan @ 2002-12-22 18:03 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1232 bytes --]

Hi everybody,

I have the following problem:


BODY FROM A PACKAGE:
--------------------

      ValidChars : String :=
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
                         &"0123456789!�$%&()=@+-;_#";
      subtype Int_Slice is Integer range ValidChars'Range;
      package My_Random is new Ada.Numerics.Discrete_Random
(Int_Slice);
 
   function Generate_FileName (Length : positive) return String is
      G : My_Random.Generator;
      I : Int_Slice;
      Result : String(1..Length);
   begin
      My_Random.Reset(G);

      for X in 1..Length
      loop
         I := My_Random.Random(G);
         Result(X) := Mnozina(I);
      end loop;
      return Result;
   end Generate_FileName;

-------------------

When I now test the procedure, I looks like the random number
generator in Ada changes its results too slowly, because I always get
at least 5 equal strings as result, then the result changes, and I get
again 5 equal string...

   for X in 1..10
   loop
      Put_Line(Generate_Filename(8));
   end loop;

The following code works like it should, but it's stupid, of course:

   for X in 1..10
   loop
      Put_Line(Generate_Filename(8));
      delay 0.01;
   end loop;


What's wrong?



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: what's wrong with random
  2002-12-22 18:03 what's wrong with random Jan
@ 2002-12-22 18:38 ` Robert A Duff
  2002-12-22 19:17   ` Jan
  0 siblings, 1 reply; 6+ messages in thread
From: Robert A Duff @ 2002-12-22 18:38 UTC (permalink / raw)


see@messagebody.com (Jan) writes:

>       ValidChars : String :=
> "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
>                          &"0123456789!�$%&()=@+-;_#";
>       subtype Int_Slice is Integer range ValidChars'Range;
>       package My_Random is new Ada.Numerics.Discrete_Random
> (Int_Slice);
>  
>    function Generate_FileName (Length : positive) return String is
>       G : My_Random.Generator;
>       I : Int_Slice;
>       Result : String(1..Length);
>    begin
>       My_Random.Reset(G);
> 
>       for X in 1..Length
>       loop
>          I := My_Random.Random(G);
>          Result(X) := Mnozina(I);
>       end loop;
>       return Result;
>    end Generate_FileName;

I think you want the generator to be global to Generate_FileName.
Otherwise, it will be reinitialized each time Generate_FileName
is called.

- Bob



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: what's wrong with random
  2002-12-22 18:38 ` Robert A Duff
@ 2002-12-22 19:17   ` Jan
  2002-12-22 20:23     ` tmoran
  2002-12-22 20:56     ` Robert A Duff
  0 siblings, 2 replies; 6+ messages in thread
From: Jan @ 2002-12-22 19:17 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1375 bytes --]

On Sun, 22 Dec 2002 18:38:33 GMT, Robert A Duff
<bobduff@shell01.TheWorld.com> wrote:

>I think you want the generator to be global to Generate_FileName.
>Otherwise, it will be reinitialized each time Generate_FileName
>is called.
>
>- Bob

What do you mean? Can you give an example?
Here is a working version:
--------
with Ada.Text_IO, Ada.Numerics.Discrete_Random;
use  Ada.Text_IO;

procedure Random_Test is 

   valid_chars : String :=
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
                     & "0123456789!�$%&()=@+-;_#";  

   subtype Int_Slice is Integer range valid_chars'range;
   package My_Random is new Ada.Numerics.Discrete_Random (Int_Slice);

   function Generate_FileName (Length : Positive) return String is 
      G      : My_Random.Generator;  
      I      : Int_Slice;  
      Result : String (1 .. Length);  
   begin
      My_Random.Reset(G);

      for X in 1..Length
            loop
         I := My_Random.Random(G);
         Result(X) := valid_chars(I);
      end loop;
      return Result;
   end Generate_FileName;

begin

   for X in 1..10
   loop
      Put_Line(Generate_Filename(8));
   end loop;

end Random_Test;

---------------

When I put

   subtype Int_Slice is Integer range valid_chars'range;
   package My_Random is new Ada.Numerics.Discrete_Random (Int_Slice);

into the function, nothing changes.

Thanks.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: what's wrong with random
  2002-12-22 19:17   ` Jan
@ 2002-12-22 20:23     ` tmoran
  2002-12-22 20:56     ` Robert A Duff
  1 sibling, 0 replies; 6+ messages in thread
From: tmoran @ 2002-12-22 20:23 UTC (permalink / raw)


> >I think you want the generator to be global to Generate_FileName.
> >Otherwise, it will be reinitialized each time Generate_FileName
>
> When I put
>
>    subtype Int_Slice is Integer range valid_chars'range;
>    package My_Random is new Ada.Numerics.Discrete_Random (Int_Slice);
>
> into the function, nothing changes.
  Making the generator global to the function means making it outside
the function, not moving the instantiation inside the function.
Every time you execute the lines:
>       G      : My_Random.Generator;
>       My_Random.Reset(G);
it resets the random generator.  You don't want it reset, you want it
to continue with the next series of random numbers.  So put those two
lines outside the function.  The first could go right after the
"package My_Random ..." line and the Reset call could go before your
function calling loop.
  The fact that a "delay" changes the behavior suggests that Reset is
setting up a new starting value based on the clock.  Since you get
5 or so duplicate strings when you don't have a "delay", it appears
that your program is generating five strings in the time between clock
ticks.  The time between ticks is dependent on hardware, OS, and
compiler, and the random number package may itself ignore the rightmost
bits, so a "clock tick" could be a long time indeed.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: what's wrong with random
  2002-12-22 19:17   ` Jan
  2002-12-22 20:23     ` tmoran
@ 2002-12-22 20:56     ` Robert A Duff
  2002-12-23 16:39       ` Jan
  1 sibling, 1 reply; 6+ messages in thread
From: Robert A Duff @ 2002-12-22 20:56 UTC (permalink / raw)


see@messagebody.com (Jan) writes:

> On Sun, 22 Dec 2002 18:38:33 GMT, Robert A Duff
> <bobduff@shell01.TheWorld.com> wrote:
> 
> >I think you want the generator to be global to Generate_FileName.
> >Otherwise, it will be reinitialized each time Generate_FileName
> >is called.
> >
> >- Bob
> 
> What do you mean? Can you give an example?

This is what I mean:

with Ada.Text_IO, Ada.Numerics.Discrete_Random;
use  Ada.Text_IO;

procedure Random_Test is 

   valid_chars : String :=
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
                     & "0123456789!�$%&()=@+-;_#";  

   subtype Int_Slice is Integer range valid_chars'range;
   package My_Random is new Ada.Numerics.Discrete_Random (Int_Slice);

   G      : My_Random.Generator;  

   function Generate_FileName (Length : Positive) return String is 
      I      : Int_Slice;  
      Result : String (1 .. Length);  
   begin
      for X in 1..Length
            loop
         I := My_Random.Random(G);
         Result(X) := valid_chars(I);
      end loop;
      return Result;
   end Generate_FileName;

begin
   My_Random.Reset(G);

   for X in 1..10
   loop
      Put_Line(Generate_Filename(8));
   end loop;

end Random_Test;



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: what's wrong with random
  2002-12-22 20:56     ` Robert A Duff
@ 2002-12-23 16:39       ` Jan
  0 siblings, 0 replies; 6+ messages in thread
From: Jan @ 2002-12-23 16:39 UTC (permalink / raw)


OK!
thanks to both

Jan

---
eMail: janp9 @ gmx.net



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2002-12-23 16:39 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-12-22 18:03 what's wrong with random Jan
2002-12-22 18:38 ` Robert A Duff
2002-12-22 19:17   ` Jan
2002-12-22 20:23     ` tmoran
2002-12-22 20:56     ` Robert A Duff
2002-12-23 16:39       ` Jan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox