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


s = 1  /* seed of the random number generator */
scale = 0
 
/* Random number from 0 to 20. */
define r() {
	auto a
	while (1) {
		/* Formula (from POSIX) for random numbers of low quality. */
		s = (s * 1103515245 + 12345) % 4294967296
		a = s / 65536       /* a in [0, 65536) */
		if (a >= 16) break  /* want a >= 65536 % 20 */
	}
	return (a % 20)
}
 
 
while (1) {
	n = r()
	n    /* print 1st number */
	if (n == 10) break
	r()  /* print 2nd number */
}
quit