Testing random selection of array element

This program tests the random selection of element from an array of string and an array of integer. It also tests the option of removing the selected elements from the list.

The blocks

The blocks

XML Code of Blocks

<xml xmlns="http://www.w3.org/1999/xhtml">
  <block type="variables_set" id="b3LH$tlID%4hM9oW[S1a" x="40" y="36">
    <field name="VAR">strArray</field>
    <value name="VALUE">
      <block type="lists_create_with" id="geyI-|2Uv6d,gv#,p^*@">
        <mutation items="3"></mutation>
        <value name="ADD0">
          <block type="text" id="XfKj?abY2XY(f+t%ZQKd">
            <field name="TEXT">a</field>
          </block>
        </value>
        <value name="ADD1">
          <block type="text" id="DJ{)fR%gb3/r`dvU!Q=v">
            <field name="TEXT">b</field>
          </block>
        </value>
        <value name="ADD2">
          <block type="text" id="`2yMiP|CW3x|-R2[h0m=">
            <field name="TEXT">c</field>
          </block>
        </value>
      </block>
    </value>
    <next>
      <block type="text_print" id="no7gRaZU}_9*xT!s:1eP">
        <value name="TEXT">
          <shadow type="text" id="{gjW[d9cA73R+%Zqq%.6">
            <field name="TEXT">abc</field>
          </shadow>
          <block type="lists_getIndex" id="n$kk7xHC|;3;4ZT1!OcD">
            <mutation statement="false" at="false"></mutation>
            <field name="MODE">GET</field>
            <field name="WHERE">RANDOM</field>
            <value name="VALUE">
              <block type="variables_get" id="RAWKiOp6Jr)%`_*dqJaV">
                <field name="VAR">strArray</field>
              </block>
            </value>
          </block>
        </value>
        <next>
          <block type="variables_set" id="-/N#^OQ~y+!@6-[Tw`dj">
            <field name="VAR">intArray</field>
            <value name="VALUE">
              <block type="lists_create_with" id="9B!UWTK,E7xdjVuP`Hc=">
                <mutation items="3"></mutation>
                <value name="ADD0">
                  <block type="math_number" id="GdP6TG|-M:66_k+99+e^">
                    <field name="NUM">1</field>
                  </block>
                </value>
                <value name="ADD1">
                  <block type="math_number" id="{66RI]o3Q9nCK#suk#2]">
                    <field name="NUM">2</field>
                  </block>
                </value>
                <value name="ADD2">
                  <block type="math_number" id="V:GW9wnhSGP8lisxThmu">
                    <field name="NUM">3</field>
                  </block>
                </value>
              </block>
            </value>
            <next>
              <block type="text_print" id="-.`:~pQG3NJd(tLP~vf|">
                <value name="TEXT">
                  <shadow type="text" id="{gjW[d9cA73R+%Zqq%.6">
                    <field name="TEXT">abc</field>
                  </shadow>
                  <block type="text_join" id="*Sc^.(_wl;XC;FKi-(w]">
                    <mutation items="1"></mutation>
                    <value name="ADD0">
                      <block type="lists_getIndex" id="pml*NWGia]4=+wFkB1m+">
                        <mutation statement="false" at="false"></mutation>
                        <field name="MODE">GET</field>
                        <field name="WHERE">RANDOM</field>
                        <value name="VALUE">
                          <block type="variables_get" id="@wNYTbKhDZODoC6B#i+U">
                            <field name="VAR">intArray</field>
                          </block>
                        </value>
                      </block>
                    </value>
                  </block>
                </value>
                <next>
                  <block type="variables_set" id="u[:#5t*wnR^cqSkEzEwZ">
                    <field name="VAR">strRemoved</field>
                    <value name="VALUE">
                      <block type="lists_getIndex" id="E.Yyt5$5WxAS[yz7V=z$">
                        <mutation statement="false" at="false"></mutation>
                        <field name="MODE">GET_REMOVE</field>
                        <field name="WHERE">RANDOM</field>
                        <value name="VALUE">
                          <block type="variables_get" id="wo5ueFa6H]{DA-U]AuJp">
                            <field name="VAR">strArray</field>
                          </block>
                        </value>
                      </block>
                    </value>
                    <next>
                      <block type="text_print" id="9Hn$ce_?J@vzi#4HG$hC">
                        <value name="TEXT">
                          <shadow type="text" id="{gjW[d9cA73R+%Zqq%.6">
                            <field name="TEXT">abc</field>
                          </shadow>
                          <block type="text_join" id="9b:8aP3M.u,,liexmooq">
                            <mutation items="8"></mutation>
                            <value name="ADD0">
                              <block type="text" id="#mM{YkZ4B^iD19zb{mFu">
                                <field name="TEXT">Removed: </field>
                              </block>
                            </value>
                            <value name="ADD1">
                              <block type="variables_get" id="7h+MKJAcpPpaJ/,_%6Y@">
                                <field name="VAR">strRemoved</field>
                              </block>
                            </value>
                            <value name="ADD2">
                              <block type="text" id="?|[VEvBA~%B}HkIoq3.2">
                                <field name="TEXT"> array size now: </field>
                              </block>
                            </value>
                            <value name="ADD3">
                              <block type="lists_length" id="I@u}=zEIBBxR55EtkQ6c">
                                <value name="VALUE">
                                  <block type="variables_get" id="]ztM69{BrNIr|U?aNqYQ">
                                    <field name="VAR">strArray</field>
                                  </block>
                                </value>
                              </block>
                            </value>
                            <value name="ADD4">
                              <block type="text" id=";WFxrBC=jh9kk!hwUEYP">
                                <field name="TEXT"> first item:  </field>
                              </block>
                            </value>
                            <value name="ADD5">
                              <block type="lists_getIndex" id="P1RbH3[fE75YAuRKIDnt">
                                <mutation statement="false" at="false"></mutation>
                                <field name="MODE">GET</field>
                                <field name="WHERE">FIRST</field>
                                <value name="VALUE">
                                  <block type="variables_get" id="$o2r(CZqD;ySK*Q$AcB$">
                                    <field name="VAR">strArray</field>
                                  </block>
                                </value>
                              </block>
                            </value>
                            <value name="ADD6">
                              <block type="text" id="sqFRO3H]Qvl8=O!wjs=G">
                                <field name="TEXT"> second item:  </field>
                              </block>
                            </value>
                            <value name="ADD7">
                              <block type="lists_getIndex" id="*9v3Z=xB^q.K?rhk1Q^u">
                                <mutation statement="false" at="true"></mutation>
                                <field name="MODE">GET</field>
                                <field name="WHERE">FROM_START</field>
                                <value name="VALUE">
                                  <block type="variables_get" id="_5.L_r`KlVf=O.kM%%]V">
                                    <field name="VAR">strArray</field>
                                  </block>
                                </value>
                                <value name="AT">
                                  <block type="math_number" id="p`dNDYd|aa@9JCk)]b26">
                                    <field name="NUM">2</field>
                                  </block>
                                </value>
                              </block>
                            </value>
                          </block>
                        </value>
                        <next>
                          <block type="variables_set" id="cZ3yDzp%@ph}a}Dx*0nA">
                            <field name="VAR">intRemoved</field>
                            <value name="VALUE">
                              <block type="lists_getIndex" id="68]bQAP^:X(Yv0E4G}{{">
                                <mutation statement="false" at="false"></mutation>
                                <field name="MODE">GET_REMOVE</field>
                                <field name="WHERE">RANDOM</field>
                                <value name="VALUE">
                                  <block type="variables_get" id="*j%#(@9Rb}sgWU..3:?4">
                                    <field name="VAR">intArray</field>
                                  </block>
                                </value>
                              </block>
                            </value>
                            <next>
                              <block type="text_print" id="zc8(zQSc/Ab)sO?Xsz:=">
                                <value name="TEXT">
                                  <shadow type="text" id="{gjW[d9cA73R+%Zqq%.6">
                                    <field name="TEXT">abc</field>
                                  </shadow>
                                  <block type="text_join" id="/QJEt_++!4:pxb2Ocv5#">
                                    <mutation items="8"></mutation>
                                    <value name="ADD0">
                                      <block type="text" id="GL3GRB,W[IC?L*1wd/rY">
                                        <field name="TEXT">Removed: </field>
                                      </block>
                                    </value>
                                    <value name="ADD1">
                                      <block type="variables_get" id="-JfT;mHhstjjuxzdb.2E">
                                        <field name="VAR">intRemoved</field>
                                      </block>
                                    </value>
                                    <value name="ADD2">
                                      <block type="text" id="g;ovpWwFS_dOarrbTb_]">
                                        <field name="TEXT"> array size now: </field>
                                      </block>
                                    </value>
                                    <value name="ADD3">
                                      <block type="lists_length" id="/QQW$-se*~XPi83O=61k">
                                        <value name="VALUE">
                                          <block type="variables_get" id="p8a96Jm-1pREtROO01#f">
                                            <field name="VAR">intArray</field>
                                          </block>
                                        </value>
                                      </block>
                                    </value>
                                    <value name="ADD4">
                                      <block type="text" id="dSn,Kxouu-0m,C$V}WD6">
                                        <field name="TEXT"> first item:  </field>
                                      </block>
                                    </value>
                                    <value name="ADD5">
                                      <block type="lists_getIndex" id="ww~s?6wP}Qa@{L^y{7|Y">
                                        <mutation statement="false" at="false"></mutation>
                                        <field name="MODE">GET</field>
                                        <field name="WHERE">FIRST</field>
                                        <value name="VALUE">
                                          <block type="variables_get" id="=,Vw9y@tX7*jUI[j4(%L">
                                            <field name="VAR">intArray</field>
                                          </block>
                                        </value>
                                      </block>
                                    </value>
                                    <value name="ADD6">
                                      <block type="text" id="G1T!Il*D1SD0iF@w[yfs">
                                        <field name="TEXT"> second item:  </field>
                                      </block>
                                    </value>
                                    <value name="ADD7">
                                      <block type="lists_getIndex" id=".~0A]{g..4e])pX6Ik[8">
                                        <mutation statement="false" at="true"></mutation>
                                        <field name="MODE">GET</field>
                                        <field name="WHERE">FROM_START</field>
                                        <value name="VALUE">
                                          <block type="variables_get" id="W{KCH`UBEJ/$#X12qz`x">
                                            <field name="VAR">intArray</field>
                                          </block>
                                        </value>
                                        <value name="AT">
                                          <block type="math_number" id="h^NUctwpdgv5K@8;62/4">
                                            <field name="NUM">2</field>
                                          </block>
                                        </value>
                                      </block>
                                    </value>
                                  </block>
                                </value>
                              </block>
                            </next>
                          </block>
                        </next>
                      </block>
                    </next>
                  </block>
                </next>
              </block>
            </next>
          </block>
        </next>
      </block>
    </next>
  </block>
</xml>

Generated Smart Pascal Code

var intArray: TIntArray;
var intRemoved: Integer;
var strArray: TStrArray;
var strRemoved: String;


function listsGetRandomItem(list: Variant; remove: Boolean): Variant;
begin
  var x := RandomInt(list.length);
  if remove then
  begin
    asm
      @result = (@list).splice(@x, 1)[0];
    end;
  end
  else
    Result := list[x];
end;


function Str(v: Variant) : string;
begin
  asm
    if (@v === 0) {
      @Result = '0';
    }
    else if (@v === '') {
      @Result = '';
    }
    else if (isNaN(@v)) {
      @Result = @v;
    }
    else {
      @Result = (@v).toString();
    }
  end;
end;



strArray := ['a', 'b', 'c'];
Console.writeln(listsGetRandomItem(Variant(strArray), false));
intArray := [1, 2, 3];
Console.writeln(Str(listsGetRandomItem(Variant(intArray), false)));
strRemoved := listsGetRandomItem(Variant(strArray), true);
Console.writeln(StrJoin([Str('Removed: '), Str(strRemoved), Str(' array size now: '), Str(strArray.length), Str(' first item:  '), Str(strArray[0]), Str(' second item:  '), Str(strArray[1])],''));
intRemoved := listsGetRandomItem(Variant(intArray), true);
Console.writeln(StrJoin([Str('Removed: '), Str(intRemoved), Str(' array size now: '), Str(intArray.length), Str(' first item:  '), Str(intArray[0]), Str(' second item:  '), Str(intArray[1])],''));
    

Copy of output from four runs of program

a 
3 
Removed: b array size now: 2 first item:  a second item:  c 
Removed: 1 array size now: 2 first item:  2 second item:  3 

a 
2 
Removed: c array size now: 2 first item:  a second item:  b 
Removed: 1 array size now: 2 first item:  2 second item:  3 

a 
3 
Removed: a array size now: 2 first item:  b second item:  c 
Removed: 2 array size now: 2 first item:  1 second item:  3 

c 
2 
Removed: c array size now: 2 first item:  a second item:  b 
Removed: 2 array size now: 2 first item:  1 second item:  3
Programming - a skill for life!

XML, image and generated Smart Pascal code of tests of lists_indexOf, lists_getIndex, lists_setIndex, lists_length and lists_getSublist blocks