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


-- help class for random number sequence
class RANDOM is
  attr seed:INT;
 
  create(seed:INT):SAME is
    res:RANDOM := new;
    res.seed := seed;
    return res;
  end;
  -- this code is taken from rand's man (C)
  next:INT is
    seed := seed * 1103515245 + 12345;
    return (seed/65536) % 32768;
  end;
end;
 
class MAIN is
  main is
    a, b :INT;
    rnd:RANDOM := #(1);
    loop
      a := rnd.next % 20;
      #OUT + a + "\n";
      if a = 10 then break!; end; -- here we break
      b := rnd.next % 20;
      #OUT + b + "\n";
    end; 
  end;
end;