This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1 @@
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.

View file

@ -0,0 +1,4 @@
---
category:
- Loop modifiers
note: Iteration

View file

@ -0,0 +1,17 @@
LoopBreakSub: PHA ;push accumulator onto stack
BreakLoop: JSR GenerateRandomNum ;routine not implemented
;generates random number and puts in memory location RandomNumber
LDA RandomNumber
JSR DisplayAccumulator ;routine not implemented
CMP #10
BEQ Break
JSR GenerateRandomNum
LDA RandomNumber
JSR DisplayAccumulator
JMP BreakLoop
Break: PLA ;restore accumulator from stack
RTS ;return from subroutine

View file

@ -0,0 +1,13 @@
main: (
INT a, b;
INT seed := 4; # chosen by a fair dice roll, guaranteed to be random c.f. http://xkcd.com/221/ #
# first random; #
WHILE
a := ENTIER (next random(seed) * 20);
print((a));
# WHILE # NOT (a = 10) DO
b := ENTIER (next random(seed) * 20);
print((b, new line))
OD;
print(new line)
)

View file

@ -0,0 +1,8 @@
BEGIN {
for (;;) {
print n = int(rand() * 20)
if (n == 10)
break
print int(rand() * 20)
}
}

View file

@ -0,0 +1,18 @@
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics.Discrete_Random;
procedure Test_Loop_Break is
type Value_Type is range 1..20;
package Random_Values is new Ada.Numerics.Discrete_Random (Value_Type);
use Random_Values;
Dice : Generator;
A, B : Value_Type;
begin
loop
A := Random (Dice);
Put_Line (Value_Type'Image (A));
exit when A = 10;
B := Random (Dice);
Put_Line (Value_Type'Image (B));
end loop;
end Test_Loop_Break;

View file

@ -0,0 +1,20 @@
integer
main(void)
{
integer a, b;
while (1) {
a = drand(19);
o_integer(a);
o_byte('\n');
if (a == 10) {
break;
}
b = drand(19);
o_integer(b);
o_byte('\n');
}
return 0;
}

View file

@ -0,0 +1,10 @@
Loop
{
Random, var, 0, 19
output = %output%`n%var%
If (var = 10)
Break
Random, var, 0, 19
output = %output%`n%var%
}
MsgBox % output

View file

@ -0,0 +1,7 @@
do
a = int(rnd * 20)
print a
if a = 10 then exit loop 'EXIT FOR works the same inside FOR loops
b = int(rnd * 20)
print b
loop

View file

@ -0,0 +1,5 @@
10 FOR l = 1 TO 20
20 IF l = 10 THEN LET l = 20: GO TO 40: REM terminate the loop
30 PRINT l
40 NEXT l
50 STOP

View file

@ -0,0 +1,6 @@
REPEAT
num% = RND(20)-1
PRINT num%
IF num%=10 THEN EXIT REPEAT
PRINT RND(20)-1
UNTIL FALSE

View file

@ -0,0 +1,6 @@
@echo off
:loop
set /a N=%RANDOM% %% 20
echo %N%
if %N%==10 exit /b
goto loop

View file

@ -0,0 +1,18 @@
#include <iostream>
#include <ctime>
#include <cstdlib>
int main()
{
srand(time(0));
while(true)
{
int a = 0 + rand() % 19;
std::cout << a << std::endl;
if (a == 10)
break;
int b = 0 + rand() % 19;
std::cout << b << std::endl;
}
return 0;
}

View file

@ -0,0 +1,17 @@
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
int main() {
int a, b;
srand(time(NULL));
while (1) {
a = rand() % 20; /* not exactly uniformly distributed, but doesn't matter */
printf("%d\n", a);
if (a == 10) break;
b = rand() % 20; /* not exactly uniformly distributed, but doesn't matter */
printf("%d\n", b);
}
return 0;
}

View file

@ -0,0 +1,58 @@
Healthy Vita-Sauce Loop - Broken.
Makes a whole lot of sauce for two people.
Ingredients.
0 g Vitamin A
1 g Vitamin B
2 g Vitamin C
3 g Vitamin D
4 g Vitamin E
5 g Vitamin F
6 g Vitamin G
7 g Vitamin H
8 g Vitamin I
9 g Vitamin J
10 g Vitamin K
11 g Vitamin L
12 g Vitamin M
13 g Vitamin N
14 g Vitamin O
15 g Vitamin P
16 g Vitamin Q
17 g Vitamin R
18 g Vitamin S
19 g Vitamin T
20 g Vitamin U
21 g Vitamin V
22 g Vitamin W
32 g Vitamin X
24 g Vitamin Y
25 g Vitamin Z
Method.
Liquify Vitamin X.
Put Vitamin N into 1st mixing bowl.
Fold Vitamin Y into 1st mixing bowl.
Liquify Vitamin Y.
Clean 1st mixing bowl.
Put Vitamin K into 1st mixing bowl.
Fold Vitamin Z into 1st mixing bowl.
Liquify Vitamin Z.
Clean 1st mixing bowl.
Put Vitamin Y into 4th mixing bowl.
Put Vitamin Z into 4th mixing bowl.
Pour contents of the 4th mixing bowl into the 2nd baking dish.
Put Vitamin A into 2nd mixing bowl. Put Vitamin B into 2nd mixing bowl. Put Vitamin C into 2nd mixing bowl. Put Vitamin D into 2nd mixing bowl. Put Vitamin E into 2nd mixing bowl. Put Vitamin F into 2nd mixing bowl. Put Vitamin G into 2nd mixing bowl. Put Vitamin H into 2nd mixing bowl. Put Vitamin I into 2nd mixing bowl. Put Vitamin J into 2nd mixing bowl. Put Vitamin K into 2nd mixing bowl. Put Vitamin L into 2nd mixing bowl. Put Vitamin M into 2nd mixing bowl. Put Vitamin N into 2nd mixing bowl. Put Vitamin O into 2nd mixing bowl. Put Vitamin P into 2nd mixing bowl. Put Vitamin Q into 2nd mixing bowl. Put Vitamin R into 2nd mixing bowl. Put Vitamin S into 2nd mixing bowl. Put Vitamin T into 2nd mixing bowl.
Verb the Vitamin V.
Mix the 2nd mixing bowl well.
Fold Vitamin U into 2nd mixing bowl.
Put Vitamin U into 3rd mixing bowl.
Remove Vitamin K from 3rd mixing bowl.
Fold Vitamin V into 3rd mixing bowl.
Put Vitamin X into 1st mixing bowl.
Put Vitamin V into 1st mixing bowl.
Verb until verbed.
Pour contents of the 1st mixing bowl into the 1st baking dish.
Serves 2.

View file

@ -0,0 +1,5 @@
(loop [[a b & more] (repeatedly #(rand-int 20))]
(println a)
(when-not (= 10 a)
(println b)
(recur more)))

View file

@ -0,0 +1,7 @@
(loop
(setq a (random 20))
(print a)
(if (= a 10)
(return))
(setq b (random 20))
(print b))

View file

@ -0,0 +1,11 @@
import std.stdio, std.random;
void main() {
while (true) {
int r = uniform(0, 20);
write(r, " ");
if (r == 10)
break;
write(uniform(0, 20), " ");
}
}

View file

@ -0,0 +1,5 @@
while True do begin
var num := RandomInt(20);
PrintLn(num);
if num=10 then Break;
end;

View file

@ -0,0 +1,15 @@
program Project5;
{$APPTYPE CONSOLE}
var
num:Integer;
begin
Randomize;
while true do
begin
num:=Random(20);
Writeln(num);
if num=10 then break;
end;
end.

View file

@ -0,0 +1,9 @@
while (true) {
def a := entropy.nextInt(20)
print(a)
if (a == 10) {
println()
break
}
println(" ", entropy.nextInt(20))
}

View file

@ -0,0 +1,9 @@
open datetime random console
loop = loop' 1
where loop' n t | r == t = writen (show r)
| else = writen (show r) `seq` loop' (n+1) t
where seed = toInt <| (ticks <| datetime.now()) * n
r = rnd seed 0 19
loop 10

View file

@ -0,0 +1,9 @@
integer i
while 1 do
i = rand(20) - 1
printf(1, "%g ", {i})
if i = 10 then
exit
end if
printf(1, "%g ", {rand(20)-1})
end while

View file

@ -0,0 +1,3 @@
[
[ 20 random [ . ] [ 10 = [ return ] when ] bi 20 random . t ] loop
] with-return

View file

@ -0,0 +1 @@
[ 20 random [ . ] [ 10 = not ] bi 20 random . ] loop

View file

@ -0,0 +1,13 @@
class ForBreak
{
public static Void main ()
{
while (true)
{
a := Int.random(0..19)
echo (a)
if (a == 10) break
echo (Int.random(0..19))
}
}
}

View file

@ -0,0 +1,14 @@
include random.fs
: main
begin 20 random dup . 10 <>
while 20 random .
repeat ;
\ use LEAVE to break out of a counted loop
: main
100 0 do
i random dup .
10 = if leave then
i random .
loop ;

View file

@ -0,0 +1,17 @@
program Example
implicit none
real :: r
integer :: a, b
do
call random_number(r)
a = int(r * 20)
write(*,*) a
if (a == 10) exit
call random_number(r)
b = int(r * 20)
write(*,*) b
end do
end program Example

View file

@ -0,0 +1,69 @@
PROGRAM LOOPBREAK
INTEGER I, RNDINT
C It doesn't matter what number you put here.
CALL SDRAND(123)
C Because FORTRAN 77 semantically lacks many loop structures, we
C have to use GOTO statements to do the same thing.
10 CONTINUE
C Print a random number.
I = RNDINT(0, 19)
WRITE (*,*) I
C If the random number is ten, break (i.e. skip to after the end
C of the "loop").
IF (I .EQ. 10) GOTO 20
C Otherwise, print a second random number.
I = RNDINT(0, 19)
WRITE (*,*) I
C This is the end of our "loop," meaning we jump back to the
C beginning again.
GOTO 10
20 CONTINUE
STOP
END
C FORTRAN 77 does not have come with a random number generator, but it
C is easy enough to type "fortran 77 random number generator" into your
C preferred search engine and to copy and paste what you find. The
C following code is a slightly-modified version of:
C
C http://www.tat.physik.uni-tuebingen.de/
C ~kley/lehre/ftn77/tutorial/subprograms.html
SUBROUTINE SDRAND (IRSEED)
COMMON /SEED/ UTSEED, IRFRST
UTSEED = IRSEED
IRFRST = 0
RETURN
END
INTEGER FUNCTION RNDINT (IFROM, ITO)
INTEGER IFROM, ITO
PARAMETER (MPLIER=16807, MODLUS=2147483647, &
& MOBYMP=127773, MOMDMP=2836)
COMMON /SEED/ UTSEED, IRFRST
INTEGER HVLUE, LVLUE, TESTV, NEXTN
SAVE NEXTN
IF (IRFRST .EQ. 0) THEN
NEXTN = UTSEED
IRFRST = 1
ENDIF
HVLUE = NEXTN / MOBYMP
LVLUE = MOD(NEXTN, MOBYMP)
TESTV = MPLIER*LVLUE - MOMDMP*HVLUE
IF (TESTV .GT. 0) THEN
NEXTN = TESTV
ELSE
NEXTN = TESTV + MODLUS
ENDIF
IF (NEXTN .GE. 0) THEN
RNDINT = MOD(MOD(NEXTN, MODLUS), ITO - IFROM + 1) + IFROM
ELSE
RNDINT = MOD(MOD(NEXTN, MODLUS), ITO - IFROM + 1) + ITO + 1
ENDIF
RETURN
END

View file

@ -0,0 +1,17 @@
while true do
a := Random(0, 19);
Print(a);
if a = 10 then
Print("\n");
break;
fi;
a := Random(0, 19);
Print("\t", a, "\n");
od;
# 11 6
# 5 8
# 1 4
# 5 10
# 1 16
# 10

View file

@ -0,0 +1,9 @@
while(1)
{
a = floor(random(19))
show_message(string(a))
if(a = 10)
break
b = floor(random(19))
show_message(string(a))
}

View file

@ -0,0 +1,18 @@
package main
import "fmt"
import "math/rand"
import "time"
func main() {
rand.Seed(time.Now().UnixNano())
for {
a := rand.Intn(20)
fmt.Println(a)
if a == 10 {
break
}
b := rand.Intn(20)
fmt.Println(b)
}
}

View file

@ -0,0 +1,9 @@
final random = new Random()
while (true) {
def random1 = random.nextInt(20)
print random1
if (random1 == 10) break
print ' '
println random.nextInt(20)
}

View file

@ -0,0 +1,9 @@
import Control.Monad
import System.Random
loopBreak n k = do
r <- randomRIO (0,n)
print r
unless (r==k) $ do
print =<< randomRIO (0,n)
loopBreak n k

View file

@ -0,0 +1 @@
loopBreak 19 10

View file

@ -0,0 +1,9 @@
1 DO i = 1, 1E20 ! "forever"
a = INT( RAN(10, 10) )
WRITE(name) a
IF( a == 10) GOTO 10
b = INT( RAN(10, 10) )
WRITE(name) b
ENDDO
10
END

View file

@ -0,0 +1,3 @@
procedure main()
while 10 ~= writes(?20-1) do write(", ",?20-1)
end

View file

@ -0,0 +1 @@
&random := integer(map("smhSMH","Hh:Mm:Ss",&clock))

View file

@ -0,0 +1,7 @@
loopexample=: verb define
while. 1 do.
smoutput n=. ?20
if. 10=n do. return. end.
smoutput ?20
end.
)

View file

@ -0,0 +1,10 @@
import java.util.Random;
Random rand = new Random();
while(true){
int a = rand.nextInt(20);
System.out.println(a);
if(a == 10) break;
int b = rand.nextInt(20);
System.out.println(b);
}

View file

@ -0,0 +1,8 @@
for (;;) {
var a = Math.floor(Math.random() * 20);
print(a);
if (a == 10)
break;
a = Math.floor(Math.random() * 20);
print(a);
}

View file

@ -0,0 +1 @@
do 20 ? int dup . 10 == if break then 20 ? int . loop

View file

@ -0,0 +1,6 @@
while num<>10
num=rnd(1)*20
print num
if num=10 then exit while
print rnd(1)*20
wend

View file

@ -0,0 +1,6 @@
while num<>10
num=int(rnd(1)*20)
print num
if num=10 then exit while
print int(rnd(1)*20)
wend

View file

@ -0,0 +1,21 @@
Section Header
+ name := TEST_LOOP_BREAK;
Section Public
- main <- (
+ a, b : INTEGER;
`srand(time(NULL))`;
{
a := `rand()`:INTEGER % 20; // not exactly uniformly distributed, but doesn't matter
a.print;
'\n'.print;
a == 10
}.until_do {
b := `rand()`:INTEGER % 20; // not exactly uniformly distributed, but doesn't matter
b.print;
'\n'.print;
}
);

View file

@ -0,0 +1,6 @@
repeat
k = math.random(19)
print(k)
if k == 10 then break end
print(math.random(19)
until false

View file

@ -0,0 +1,12 @@
define(`randSeed',141592653)dnl
define(`setRand',
`define(`randSeed',ifelse(eval($1<10000),1,`eval(20000-$1)',`$1'))')dnl
define(`rand_t',`eval(randSeed^(randSeed>>13))')dnl
define(`random',
`define(`randSeed',eval((rand_t^(rand_t<<18))&0x7fffffff))randSeed')dnl
dnl
define(`loopbreak',`define(`a',eval(random%20))`a='a
ifelse(a,10,`',`define(`b',eval(random%20))`b='b
loopbreak')')dnl
dnl
loopbreak

View file

@ -0,0 +1,9 @@
while (1)
a = random(20) - 1;
player:tell(a);
if (a == 10)
break;
endif
b = random(20) - 1;
player:tell(b);
endwhile

View file

@ -0,0 +1,15 @@
BREAKLOOP
NEW A,B
SET A=""
FOR Q:A=10 DO
.SET A=$RANDOM(20)
.WRITE !,A
.Q:A=10
.SET B=$RANDOM(20)
.WRITE ?6,B
KILL A,B
QUIT
;A denser version that doesn't require two tests
NEW A,B
FOR SET A=$RANDOM(20) WRITE !,A QUIT:A=10 SET B=$RANDOM(20) WRITE ?6,B
KILL A,B QUIT

View file

@ -0,0 +1,9 @@
r := rand( 0 .. 19 ):
do
n := r();
printf( "%d\n", n );
if n = 10 then
break
end if;
printf( "%d\n", r() );
end do:

View file

@ -0,0 +1,3 @@
While[(Print[#];#!=10)&[RandomIntger[{0,19}]],
Print[RandomInteger[{0,19}]
]

View file

@ -0,0 +1,36 @@
/* To exit the innermost block, use return(<value>) */
block([n],
do (
n: random(20),
ldisp(n),
if n = 10 then return(),
n: random(20),
ldisp(n)
)
)$
/* To exit any level of block, use catch(...) and throw(<value>);
they are not used for catching exceptions, but for non-local
return. Use errcatch(...) for exceptions. */
block([n],
catch(
do (
n: random(20),
ldisp(n),
if n = 10 then throw('done),
n: random(20),
ldisp(n)
)
)
)$
/* There is also break(<value>, ...) in Maxima. It makes Maxima
stop the evaluation and enter a read-eval loop where one can change
variable values, then return to the function after exit; For example */
block([x: 1], break(), ldisp(x));
> x: 2;
> exit;
2

View file

@ -0,0 +1,17 @@
MODULE Break EXPORTS Main;
IMPORT IO, Fmt, Random;
VAR a,b: INTEGER;
BEGIN
WITH rand = NEW(Random.Default).init() DO
LOOP
a := rand.integer(min := 0, max := 19);
IO.Put(Fmt.Int(a) & "\n");
IF a = 10 THEN EXIT END;
b := rand.integer(min := 0, max := 19);
IO.Put(Fmt.Int(b) & "\n");
END;
END;
END Break.

View file

@ -0,0 +1,8 @@
while (true) {
$a = rand(0,19);
echo "$a\n";
if ($a == 10)
break;
$b = rand(0,19);
echo "$b\n";
}

View file

@ -0,0 +1,9 @@
while (1) {
my $a = int(rand(20));
print "$a\n";
if ($a == 10) {
last;
}
my $b = int(rand(20));
print "$b\n";
}

View file

@ -0,0 +1,5 @@
(use R
(loop
(println (setq R (rand 1 19)))
(T (= 10 R))
(println (rand 1 19)) ) )

View file

@ -0,0 +1,2 @@
(until (= 10 (println (rand 1 19)))
(println (rand 1 19)) )

View file

@ -0,0 +1,9 @@
import random
while True:
a = random.randrange(20)
print a
if a == 10:
break
b = random.randrange(20)
print b

View file

@ -0,0 +1,12 @@
sample0to19 <- function() sample(0L:19L, 1,replace=TRUE)
repeat
{
result1 <- sample0to19()
if (result1 == 10L)
{
print(result1)
break
}
result2 <- sample0to19()
cat(result1, result2, "\n")
}

View file

@ -0,0 +1,10 @@
/*REXX program demonstrates a FOREVER DO loop with a test to LEAVE. */
do forever /*perform until da cows come home*/
a=random(19) /*same as: random(0,19) */
call charout ,right(a,5) /*show A right-justified, col. 1.*/
if a==10 then leave /*Random#=10? Then cows went home*/
b=random(19) /*same as: random(0,19) */
say right(b,5) /*show B right-justified, col. 2.*/
end /*forever*/
/*stick a fork in it, we're done.*/

View file

@ -0,0 +1,8 @@
#lang racket
(let loop ()
(let/ec break
(define a (random 20))
(displayln a)
(when (= a 10) (break))
(displayln (random 20))
(loop)))

View file

@ -0,0 +1,9 @@
loop do
a = rand(20)
puts a
if a == 10
break
end
b = rand(20)
puts b
end

View file

@ -0,0 +1,29 @@
-- 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;

View file

@ -0,0 +1,19 @@
scala> import util.control.Breaks.{breakable, break}
import util.control.Breaks.{breakable, break}
scala> import util.Random
import util.Random
scala> breakable {
| while(true) {
| val a = Random.nextInt(20)
| println(a)
| if(a == 10)
| break
| val b = Random.nextInt(20)
| println(b)
| }
| }
5
4
10

View file

@ -0,0 +1,6 @@
(let loop ((first (random 20)))
(print first)
(if (not (= first 10))
(begin
(print (random 20))
(loop (random 20)))))

View file

@ -0,0 +1,8 @@
(call/cc
(lambda (break)
(let loop ((first (random 20)))
(print first)
(if (= first 10)
(break))
(print (random 20))
(loop (random 20)))))

View file

@ -0,0 +1,11 @@
[
|a b done|
a := Random nextIntegerBetween:0 and:19.
Stdout print: a; cr.
(done := (a == 10)) ifFalse:[
b := Random nextIntegerBetween:0 and:19.
Stdout print:' '; print: b; cr.
].
done
] whileFalse

View file

@ -0,0 +1,7 @@
[:exit |
|first|
Stdout printCR: (first := Random nextIntegerBetween:0 and:19).
first == 10 ifTrue:[ exit value:nil ].
Stdout print:' '; printCR: (Random nextIntegerBetween:0 and:19).
] loopWithExit.

View file

@ -0,0 +1,9 @@
while true {
set a [expr int(20*rand())]
puts $a
if {$a == 10} {
break
}
set b [expr int(20*rand())]
puts $b
}