How to resolve the algorithm Loops/Break step by step in the dc programming language

Published on 12 May 2024 09:40 PM
#Dc

How to resolve the algorithm Loops/Break step by step in the dc programming language

Table of Contents

Problem Statement

Show a loop which prints random numbers (each number newly generated each loop) from 0 to 19 (inclusive). If a number is 10, stop the loop after printing it, and do not generate any further numbers.
Otherwise, generate and print a second random number before restarting the loop.
If the number 10 is never generated as the first number in a loop, loop forever.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Loops/Break step by step in the dc programming language

Source code in the dc programming language

1 ss  [s = seed of the random number generator]sz
0k    [scale = 0]sz

[Function r: Push a random number from 0 to 20.]sz
[
 [2Q]SA
 [
  [Formula (from POSIX) for random numbers of low quality.]sz
  ls 1103515245 * 12345 + 4294967296 % d ss  [Compute next s]sz
  65536 /     [it = s / 65536]sz
  d 16 !>A    [Break loop if 16 <= it]sz
  sz 0 0 =B   [Forget it, continue loop]sz
 ]SB 0 0 =B
 20 %         [Push it % 20]sz
 LA sz LB sz  [Restore A, B]sz
]sr


[2Q]sA
[
 0 0 =r p     [Print 1st number.]sz
 10 =A        [Break if 10 == it.]sz
 0 0 =r p sz  [Print 2nd number.]sz
 0 0 =B       [Continue loop.]sz
]sB 0 0 =B

  

You may also check:How to resolve the algorithm Find the intersection of two lines step by step in the C++ programming language
You may also check:How to resolve the algorithm Floyd-Warshall algorithm step by step in the OCaml programming language
You may also check:How to resolve the algorithm Window creation/X11 step by step in the Julia programming language
You may also check:How to resolve the algorithm List rooted trees step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Idiomatically determine all the characters that can be used for symbols step by step in the Nim programming language