I want to fill a matrix randomly with True and False.

The code below does produce a matrix where the proportion of True is roughly p, but all the Trues are at the beginning, and all the False at the end (so it looks like it generates the same random number over and over).

Can you tell me how to fix this?

Thank you,

Alastair

program GeoTournament;

const
  n   = 20;
  p   = 0.5;

var
  Adj : array[1..n,1..n] of Boolean;
  i,j  : integer;


BEGIN
  randomize;

  FOR i := 1 TO n-1 DO
    FOR j := i+1 TO n DO
      IF random <= p THEN
        BEGIN
          Adj[i,j] := TRUE;
          Adj[j,i] := FALSE;
        END
      ELSE
        BEGIN
          Adj[i,j] := FALSE;
          Adj[i,j] := TRUE;
        END;

END.

i dont know about pascal but in VB you use the Rnd() function to generate random numbers, but it always seems to generate the same ones. In VB to make it more random you have to add Randomize() at the start of your code.

maybe the same thing happens in pascal?

Yes, it is the same. And (at least by what I see now) his code does start with randomize.

In computers, there is no such thing as a truly random number. Delphi's random number generator is notoriously wimpy.

You'll have to google "delphi random number generator" or something similar for a better answer.

Good luck.

And (at least by what I see now) his code does start with randomize.

sorry, i didnt read it properley ;)

> sorry, i didnt read it properley

I do that all the time... :$

Perhaps there is an error in line 25. Try changing to

Adj[j,i] := TRUE;

Don't know if you are still looking for a better solution Alastair but here is one anyway. It uses Delphi but given that it relies on the CoCreateGUID function to generate random sequences you should be able to implement something similar in Pascal on Windows as well as in NIX.

As others have pointed out software based random numbers are never truly random - an algorithm of some description is used to produce them.

Algorithm=Logic.

Logic != Random.

If you have trawled the net or read up on random numbers in any good patterns book you will have run into a number of algorithms that help improve things - ususally by using a more complex algorithm etc. However, you don't really need to implement a more complex algorithm yourself. The algorithm used to generate GUIDs is frighteningly complex and, best of all, it is there!

That is what I have used. Download the attachment and enjoy.

Perhaps there is an error in line 25. Try changing to

Adj[j,i] := TRUE;

Yes, that's it. The code works well now.

Thanks for the Delphi code posted above.

Regards,

Alastair

Altough this subject is solved, maybe a small note on improving the algorithm. There's no such thing as true randomness through an algorithm, however with pseudo-randomness of a good algorithm such as the Mersenne Twister and good entropy you go a long way. A lot of gambling sites use the Merssene Twister algorithm for randomness and the entropy is being determined by the mouse movements of users connected to their network. The entropy in your algorithm is being determined by the 'randomize' function and is largely deducted from the system-clock and therefor predictable. Since I don't know specifically in what kind of application it's used and what the requirements are, I thought I share this with y'all anyway.

Read about it here:

http://en.wikipedia.org/wiki/Pseudorandom_number_generator
http://en.wikipedia.org/wiki/Mersenne_twister


I want to fill a matrix randomly with True and False.

The code below does produce a matrix where the proportion of True is roughly p, but all the Trues are at the beginning, and all the False at the end (so it looks like it generates the same random number over and over).

Can you tell me how to fix this?

Thank you,

Alastair

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.