Task

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.



Related tasks

   Loop over multiple arrays simultaneously
   Loops/Break
   Loops/Continue
   Loops/Do-while
   Loops/Downward for
   Loops/For
   Loops/For with a specified step
   Loops/Foreach
   Loops/Increment loop index within loop body
   Loops/Infinite
   Loops/N plus one half
   Loops/Nested
   Loops/While
   Loops/with multiple ranges


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