Selecting array elements randomly

This example uses the lists_getIndex block with the options GET_REMOVE and RANDOM to make selections of facts contained in an array of string. We use a hash instead of a comma to separate the initial list items in the string strFacts because one of the facts has several commas within it. The program works as intended when the HTML file output by Smart Mobile Studio is opened in Microsoft Edge or Internet Explorer. (In the other browsers that we have tried the prompt appears before the execution of lines of code preceding the prompt statement).

The blocks

The blocks

XML Code of Blocks

<xml xmlns="http://www.w3.org/1999/xhtml">
  <block type="variables_set" id="z!p%dR9?gxwo7@qS,+F(" x="29" y="67">
    <field name="VAR">strFacts</field>
    <value name="VALUE">
      <block type="text" id="!k~@z:#m`WJSTs6iDD@P">
        <field name="TEXT">Arsenal won the FA Cup in 2017.#Kyiv is the capital of Ukraine.#In May 2017 the president of USA was Donald Trump.#Brexit means Brexit.#The population of England is approximately ten times that of Scotland.# Among the celebrity deaths in 2016 were Mohammed Ali, David Bowie, Fidel Castro, Carrie Fisher, Zsa Zsa Gabor, John Glenn, George Michael, Prince, Debbie Reynolds and Alan Rickman.#The Milky Way is a spiral galaxy with a diameter at least 100,000 light-years.#The Amazon basin, the largest drainage basin in the world, has an area of about 7 million square kilometres.#The rhino can run up to 40 miles per hour.#The California redwood can live for nearly 2000 years and reach up to more than 100m.</field>
      </block>
    </value>
    <next>
      <block type="variables_set" id="g]O=,KSFM`S2U`Fs2Z!h">
        <field name="VAR">strFactsArray</field>
        <value name="VALUE">
          <block type="lists_split" id="9Bdp76PL.2W5he^nZ,]V">
            <mutation mode="SPLIT"></mutation>
            <field name="MODE">SPLIT</field>
            <value name="INPUT">
              <block type="variables_get" id="p0Y/{K:w%-*jde^6aanB">
                <field name="VAR">strFacts</field>
              </block>
            </value>
            <value name="DELIM">
              <shadow type="text" id="nt~iOUL($$!aIz95T4E?">
                <field name="TEXT">#</field>
              </shadow>
            </value>
          </block>
        </value>
        <next>
          <block type="variables_set" id="rI_7O87g@h~^EF8D$R*P">
            <field name="VAR">strResponse</field>
            <value name="VALUE">
              <block type="text" id="PHxQl{:yjKzFZF:G+%8K">
                <field name="TEXT">y</field>
              </block>
            </value>
            <next>
              <block type="text_print" id="Zy+k3WQde:y+|o1W4mdE">
                <value name="TEXT">
                  <shadow type="text" id="cE)i5/X{!*{fg7LA1Rb.">
                    <field name="TEXT">abc</field>
                  </shadow>
                  <block type="lists_getIndex" id="mq=5,#hpjA[eSz_^ffJH">
                    <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=")yTleE7T]74I[LF+n@l4">
                        <field name="VAR">strFactsArray</field>
                      </block>
                    </value>
                  </block>
                </value>
                <next>
                  <block type="controls_whileUntil" id="lS=+W]*7yC~xP37Oc%O6">
                    <field name="MODE">WHILE</field>
                    <value name="BOOL">
                      <block type="logic_operation" id="XX=hy8TM.H2yVP~B]QlH">
                        <field name="OP">AND</field>
                        <value name="A">
                          <block type="logic_compare" id="LzOcZvU%}4QE#zimv9k#">
                            <field name="OP">GTE</field>
                            <value name="A">
                              <block type="lists_length" id="fKS8G~%jpRRW+xIO|+*v">
                                <value name="VALUE">
                                  <block type="variables_get" id="I0KCK=H%ysKEHD[p[eTR">
                                    <field name="VAR">strFactsArray</field>
                                  </block>
                                </value>
                              </block>
                            </value>
                            <value name="B">
                              <block type="math_number" id="H!i97;bV`pfr^Ps7^58z">
                                <field name="NUM">1</field>
                              </block>
                            </value>
                          </block>
                        </value>
                        <value name="B">
                          <block type="logic_compare" id="BBldN0$|Sa%=XH4]AqjM">
                            <field name="OP">EQ</field>
                            <value name="A">
                              <block type="text_changeCase" id="F.N7hUO(c0.gS=1OtQvy">
                                <field name="CASE">LOWERCASE</field>
                                <value name="TEXT">
                                  <shadow type="text" id="pNfock(SRdS}?wvv!{*j">
                                    <field name="TEXT">abc</field>
                                  </shadow>
                                  <block type="variables_get" id="~RZg)`[9/6Mn8-IDFOqt">
                                    <field name="VAR">strResponse</field>
                                  </block>
                                </value>
                              </block>
                            </value>
                            <value name="B">
                              <block type="text" id="q79Qs:;_QIpS:(%3oE?f">
                                <field name="TEXT">y</field>
                              </block>
                            </value>
                          </block>
                        </value>
                      </block>
                    </value>
                    <statement name="DO">
                      <block type="variables_set" id="4r.;TWRU;.RD}y)=i.yd">
                        <field name="VAR">strResponse</field>
                        <value name="VALUE">
                          <block type="text_prompt_ext" id="!Zj3t3CeqmJ4~O|hri6o">
                            <mutation type="TEXT"></mutation>
                            <field name="TYPE">TEXT</field>
                            <value name="TEXT">
                              <shadow type="text" id="sFk~BY9o7OlcmKJ(TsP%">
                                <field name="TEXT">Continue? y or n</field>
                              </shadow>
                            </value>
                          </block>
                        </value>
                        <next>
                          <block type="controls_if" id="0-I(nYAF/$d@g|C{jFKP">
                            <value name="IF0">
                              <block type="logic_compare" id="tCGUk9;teQtTIfC2N]8v">
                                <field name="OP">EQ</field>
                                <value name="A">
                                  <block type="text_changeCase" id="3^g%X2sE}*|W#|3;.bk2">
                                    <field name="CASE">LOWERCASE</field>
                                    <value name="TEXT">
                                      <shadow type="text" id="akbtEHVl_*yEH#=;yx{H">
                                        <field name="TEXT">abc</field>
                                      </shadow>
                                      <block type="variables_get" id="qr)EsUB?xJ/1QsEAO9@I">
                                        <field name="VAR">strResponse</field>
                                      </block>
                                    </value>
                                  </block>
                                </value>
                                <value name="B">
                                  <block type="text" id="O?3Me0N?f.H}8pu}r_8?">
                                    <field name="TEXT">y</field>
                                  </block>
                                </value>
                              </block>
                            </value>
                            <statement name="DO0">
                              <block type="text_print" id="-tY!IbmhgCKUn1PBJ(P,">
                                <value name="TEXT">
                                  <shadow type="text" id="cE)i5/X{!*{fg7LA1Rb.">
                                    <field name="TEXT">abc</field>
                                  </shadow>
                                  <block type="lists_getIndex" id="]?^1$7Y.J}%b(?qxF=H0">
                                    <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="]pC!kFmBB;uY(ZRVavP}">
                                        <field name="VAR">strFactsArray</field>
                                      </block>
                                    </value>
                                  </block>
                                </value>
                              </block>
                            </statement>
                          </block>
                        </next>
                      </block>
                    </statement>
                  </block>
                </next>
              </block>
            </next>
          </block>
        </next>
      </block>
    </next>
  </block>
</xml>

Generated Smart Pascal Code

var strFactsArray: TStrArray;
var strResponse: String;
var strFacts: 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;



strFacts := 'Arsenal won the FA Cup in 2017.#Kyiv is the capital of Ukraine.#In May 2017 the president of USA was Donald Trump.#Brexit means Brexit.#The population of England is approximately ten times that of Scotland.# Among the celebrity deaths in 2016 were Mohammed Ali, David Bowie, Fidel Castro, Carrie Fisher, Zsa Zsa Gabor, John Glenn, George Michael, Prince, Debbie Reynolds and Alan Rickman.#The Milky Way is a spiral galaxy with a diameter at least 100,000 light-years.#The Amazon basin, the largest drainage basin in the world, has an area of about 7 million square kilometres.#The rhino can run up to 40 miles per hour.#The California redwood can live for nearly 2000 years and reach up to more than 100m.';
strFactsArray := StrSplit(strFacts, '#');
strResponse := 'y';
Console.writeln(listsGetRandomItem(Variant(strFactsArray), true));
while ((strFactsArray.length >= 1) and (LowerCase(strResponse) = 'y')) do
begin
  strResponse := prompt('Continue? y or n');
  if ((LowerCase(strResponse) = 'y')) then
  begin
    Console.writeln(listsGetRandomItem(Variant(strFactsArray), true));
  end;
end;
   

Copy of output from Microsoft Edge

This was the output after user responses of one y followed by a n.

Among the celebrity deaths in 2016 were Mohammed Ali, David Bowie, Fidel Castro,
Carrie Fisher, Zsa Zsa Gabor, John Glenn, George Michael, Prince, Debbie Reynolds
and Alan Rickman.
The California redwood can live for nearly 2000 years and reach up to more than
100m.
Programming - a skill for life!

Suggestions, with examples such as Caesar cipher and Fibonacci sequence, for Blockly programs that you could develop