Using the random integer block to create a random identifier

This example uses the "soup" of characters chosen by the Blockly developers to be selected at random for each of the 20 positions in the ID string. We employ to good effect the "append text" and "random integer" blocks. See the following page for an application of the code.

The blocks

The blocks

XML Code of Blocks

<xml xmlns="http://www.w3.org/1999/xhtml">
  <block type="variables_set" id="y@B206fW$hJcUAKrdVMA" x="13" y="13">
    <field name="VAR">strID</field>
    <value name="VALUE">
      <block type="text" id="}wAZlM.QZ@5h^(;wYX=w">
        <field name="TEXT"></field>
      </block>
    </value>
    <next>
      <block type="variables_set" id="RwJ|CL-!w{Rd/d2+}2|j">
        <field name="VAR">strSoup</field>
        <value name="VALUE">
          <block type="text" id="@m@/V#1XA=qg|E4f.sG|">
            <field name="TEXT">!#$%()*+,-./:;=?@[]^_`{|}~ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789</field>
          </block>
        </value>
        <next>
          <block type="variables_set" id="]FoKVB6ae5Uw+IV`T=~M">
            <field name="VAR">intSoupLength</field>
            <value name="VALUE">
              <block type="text_length" id="(BT1wh(Ww.M^)%Q09X#C">
                <value name="VALUE">
                  <shadow id="f4Q;E/-1?|zt#4CS5pUn" type="text">
                    <field name="TEXT">abc</field>
                  </shadow>
                  <block type="variables_get" id="X^fIk7coVVDj)YSTli~`">
                    <field name="VAR">strSoup</field>
                  </block>
                </value>
              </block>
            </value>
            <next>
              <block type="controls_for" id="Q]g49@p=G0M^;WPTKBa*">
                <field name="VAR">i</field>
                <value name="FROM">
                  <shadow type="math_number" id="f?F()_ADU@sAxZ*r|Yjf">
                    <field name="NUM">1</field>
                  </shadow>
                </value>
                <value name="TO">
                  <shadow type="math_number" id="P]SWYlbyde]niwJ|,L0d">
                    <field name="NUM">20</field>
                  </shadow>
                </value>
                <value name="BY">
                  <shadow type="math_number" id="%3L4!AUF1U4OhBB!pqp-">
                    <field name="NUM">1</field>
                  </shadow>
                </value>
                <statement name="DO">
                  <block type="text_append" id=";Ze^QnKANRoE(AT1(|G2">
                    <field name="VAR">strID</field>
                    <value name="TEXT">
                      <shadow id="g@S#k!?=7X2rr!{?]3Al" type="text">
                        <field name="TEXT"></field>
                      </shadow>
                      <block type="text_charAt" id="(H`8X`-_9KwGP#x7Y,65">
                        <mutation at="true"></mutation>
                        <field name="WHERE">FROM_START</field>
                        <value name="VALUE">
                          <block type="variables_get" id=".Mi.t.co)7X|Bol;fwL=">
                            <field name="VAR">strSoup</field>
                          </block>
                        </value>
                        <value name="AT">
                          <block type="math_random_int" id="TalNI@Lqc~?`#4lD]s#P">
                            <value name="FROM">
                              <shadow type="math_number" id="wrDu~bH9*@4gB%R6N]l|">
                                <field name="NUM">1</field>
                              </shadow>
                            </value>
                            <value name="TO">
                              <shadow id="!$@-qA_Zx.Tmuvsq(jLj" type="math_number">
                                <field name="NUM">100</field>
                              </shadow>
                              <block type="variables_get" id="IR[/HwWloX1T^^gMKu^L">
                                <field name="VAR">intSoupLength</field>
                              </block>
                            </value>
                          </block>
                        </value>
                      </block>
                    </value>
                  </block>
                </statement>
                <next>
                  <block type="text_print" id="sZ~Dc;1Cvyn@M`s0~=Gi">
                    <value name="TEXT">
                      <shadow id="BBzwe)*}PiaiK[(+QMoE" type="text">
                        <field name="TEXT">abc</field>
                      </shadow>
                      <block type="variables_get" id="7I;6V8bjT`MG[LI$OZ,/">
                        <field name="VAR">strID</field>
                      </block>
                    </value>
                  </block>
                </next>
              </block>
            </next>
          </block>
        </next>
      </block>
    </next>
  </block>
</xml>

Generated Smart Pascal Code

var intSoupLength: Integer;
var strID: String;
var strSoup: String;


function RandomIntBetween(a, b: integer): integer;
begin
  if (a > b) then
    begin
      // Swap a and b to ensure a is smaller.
      var c := a;
      a := b;
      b := c;
    end;
  result := Floor(Random() * (b - a + 1) + a)
end;



strID := '';
strSoup := '!#$%()*+,-./:;=?@[]^_`{|}~ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
intSoupLength := length(strSoup);
for var i := 1 to 20 do
begin
  strID += CharAt(strSoup, (RandomIntBetween(1, intSoupLength) - 1) + 1);
end;
Console.writeln(strID);
    

Copy of Output

0!%~~p.mMf-[#`_@nV2o
Programming - a skill for life!

XML, images and generated Smart Pascal code of examples including loops, arrays, validation and recursion