Recent

Author Topic: design.rcbd/random  (Read 918 times)

flori

  • Full Member
  • ***
  • Posts: 196
design.rcbd/random
« on: May 05, 2021, 05:44:01 pm »
Hi everyone!
I would like to generate randomized block design in lazarus. Does anyone know how to mix(randomizes) the numbers within the block?

For example:
Treatments: 10
Replication: 2
-------------------------
Results:
"1"  "2"  "5"  "10" "3"  "4"  "6"  "8"  "9"  "7"
"6"  "4"  "3"  "7"  "10" "9"  "5"  "1"  "2"  "8"

rows= replication,  col= trt
 

unfortunately, Rstat software sometimes mixes badly (design.rcbd).
I attached my demo.
Thank you :)


Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: design.rcbd/random
« Reply #1 on: May 05, 2021, 05:49:15 pm »
I have some code to have a "random array".
Code: Pascal  [Select][+][-]
  1. {
  2.   Returns an array of aSize random integers without duplicates ranging from
  3.   aFrom (inclusive) to aTo (exclusive).
  4.   - aFrom and aTo must be positive integers
  5.   - aTo must be > aFrom
  6.   - aSize must be <= aTo - aFrom
  7.   - if either of these conditions is not met, an empty array will be returned.
  8. }
  9. function RandomArray(aFrom, aTo, aSize: Integer): TIntegerDynArray; overload;
  10. var
  11.   i, j, temp, Range: Integer;
  12. begin
  13.   Range := (aTo - aFrom);
  14.   if (aFrom < 0) or (aFrom >= aTo) or (aSize > Range) then
  15.     Exit(nil);
  16.   SetLength(Result, Range);
  17.   for i := Low(Result) to High(Result) do Result[i] := aFrom + i;
  18.   // Use te Sattolo algorithm to shuffle the array
  19.   // https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#Sattolo.27s_algorithm
  20.   i := Length(Result);
  21.   while i > 0 do
  22.   begin
  23.     Dec(i);
  24.     j := Random(i);
  25.     temp := Result[i];
  26.     Result[i] := Result[j];
  27.     Result[j] := temp;
  28.   end;
  29.   if (aSize <> Range) then
  30.     SetLength(Result, aSize);
  31. end;
  32.  
  33. function RandomArray(aFrom, aTo: Integer): TIntegerDynArray; inline; overload;
  34. begin
  35.   Result := RandomArray(aFrom, aTo, (aTo - aFrom));
  36. end;
  37.  
  38. function RandomArray(Range: Integer): TIntegerDynArray; inline; overload;
  39. begin
  40.   Result := RandomArray(0, Range, Range);
  41. end;

As you can see it uses Fisher Yates Shuffle algorithm.


Bart

flori

  • Full Member
  • ***
  • Posts: 196
Re: design.rcbd/random
« Reply #2 on: May 05, 2021, 06:57:20 pm »
Thank you:
The results>

Code: Pascal  [Select][+][-]
  1. begin
  2.     randomize;
  3.     n:=0;
  4.     write('rep: ');
  5.   readln(n);
  6.  
  7.  
  8.  
  9.      //
  10.       for  d:=1 to n do
  11.     begin
  12.        a:=a.Create(1,2,3,4,5,6,7,8,9,10);
  13.           i := length(a);
  14.        while i > 0 do
  15.         begin
  16.       dec(i);
  17.       j :=randomrange(Low(a),i);
  18.       t:=a[i];a[i]:=a[j];a[j]:=t;
  19.       write(a[i],' ');
  20.       end;
  21.        writeln('');
  22.        writeln('---------------');
  23.        end;
                               

 

TinyPortal © 2005-2018