Loops/Break: Difference between revisions
Content added Content deleted
(→{{header|J}}: some alternatives) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 30: | Line 30: | ||
=={{header|11l}}== |
=={{header|11l}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">L |
||
V a = random:(20) |
V a = random:(20) |
||
print(a) |
print(a) |
||
Line 36: | Line 36: | ||
L.break |
L.break |
||
V b = random:(20) |
V b = random:(20) |
||
print(b)</ |
print(b)</syntaxhighlight> |
||
=={{header|360 Assembly}}== |
=={{header|360 Assembly}}== |
||
< |
<syntaxhighlight lang="360asm">* Loops Break 15/02/2017 |
||
LOOPBREA CSECT |
LOOPBREA CSECT |
||
USING LOOPBREA,R13 base register |
USING LOOPBREA,R13 base register |
||
Line 77: | Line 77: | ||
XDEC DS CL12 |
XDEC DS CL12 |
||
YREGS |
YREGS |
||
END LOOPBREA</ |
END LOOPBREA</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 94: | Line 94: | ||
=={{header|6502 Assembly}}== |
=={{header|6502 Assembly}}== |
||
Code is called as a subroutine (i.e. JSR LoopBreakSub). Specific OS/hardware routines for generating random numbers and printing are left unimplemented. |
Code is called as a subroutine (i.e. JSR LoopBreakSub). Specific OS/hardware routines for generating random numbers and printing are left unimplemented. |
||
< |
<syntaxhighlight lang="6502asm">LoopBreakSub: PHA ;push accumulator onto stack |
||
Line 110: | Line 110: | ||
Break: PLA ;restore accumulator from stack |
Break: PLA ;restore accumulator from stack |
||
RTS ;return from subroutine</ |
RTS ;return from subroutine</syntaxhighlight> |
||
=={{header|AArch64 Assembly}}== |
=={{header|AArch64 Assembly}}== |
||
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
||
<syntaxhighlight lang="aarch64 assembly"> |
|||
<lang AArch64 Assembly> |
|||
/* ARM assembly AARCH64 Raspberry PI 3B */ |
/* ARM assembly AARCH64 Raspberry PI 3B */ |
||
/* program loopbreak64.s */ |
/* program loopbreak64.s */ |
||
Line 210: | Line 210: | ||
/* for this file see task include a file in language AArch64 assembly */ |
/* for this file see task include a file in language AArch64 assembly */ |
||
.include "../includeARM64.inc" |
.include "../includeARM64.inc" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{output}} |
{{output}} |
||
<pre> |
<pre> |
||
Line 227: | Line 227: | ||
</pre> |
</pre> |
||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">PROC Main() |
||
BYTE v |
BYTE v |
||
Line 239: | Line 239: | ||
OD |
OD |
||
PrintE("After loop") |
PrintE("After loop") |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Break.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Break.png Screenshot from Atari 8-bit computer] |
||
Line 257: | Line 257: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO; |
||
with Ada.Numerics.Discrete_Random; |
with Ada.Numerics.Discrete_Random; |
||
Line 274: | Line 274: | ||
Put_Line (Value_Type'Image (B)); |
Put_Line (Value_Type'Image (B)); |
||
end loop; |
end loop; |
||
end Test_Loop_Break;</ |
end Test_Loop_Break;</syntaxhighlight> |
||
=={{header|Aime}}== |
=={{header|Aime}}== |
||
< |
<syntaxhighlight lang="aime">integer |
||
main(void) |
main(void) |
||
{ |
{ |
||
Line 296: | Line 296: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|ALGOL 60}}== |
=={{header|ALGOL 60}}== |
||
{{works with|ALGOL 60|OS/360}} |
{{works with|ALGOL 60|OS/360}} |
||
< |
<syntaxhighlight lang="algol60">'BEGIN' 'COMMENT' Loops/Break - ALGOL60 - 18/06/2018; |
||
'INTEGER' SEED; |
'INTEGER' SEED; |
||
'INTEGER' 'PROCEDURE' RANDOM(N); |
'INTEGER' 'PROCEDURE' RANDOM(N); |
||
Line 324: | Line 324: | ||
LAB: |
LAB: |
||
SYSACT(1,14,1); 'COMMENT' skip line; |
SYSACT(1,14,1); 'COMMENT' skip line; |
||
'END'</ |
'END'</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 336: | Line 336: | ||
{{wont work with|ALGOL 68G|Any - in a68G next random takes no seed argument}} |
{{wont work with|ALGOL 68G|Any - in a68G next random takes no seed argument}} |
||
{{works with|ELLA ALGOL 68|Any (with appropriate job cards)}} |
{{works with|ELLA ALGOL 68|Any (with appropriate job cards)}} |
||
< |
<syntaxhighlight lang="algol68">main: ( |
||
INT a, b; |
INT a, b; |
||
INT seed := 4; # chosen by a fair dice roll, guaranteed to be random c.f. http://xkcd.com/221/ # |
INT seed := 4; # chosen by a fair dice roll, guaranteed to be random c.f. http://xkcd.com/221/ # |
||
Line 348: | Line 348: | ||
OD; |
OD; |
||
print(new line) |
print(new line) |
||
)</ |
)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="height:25ex;overflow:scroll"> |
<pre style="height:25ex;overflow:scroll"> |
||
Line 383: | Line 383: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
< |
<syntaxhighlight lang="applescript">repeat |
||
set a to random number from 0 to 19 |
set a to random number from 0 to 19 |
||
if a is 10 then |
if a is 10 then |
||
Line 391: | Line 391: | ||
set b to random number from 0 to 19 |
set b to random number from 0 to 19 |
||
log a & b |
log a & b |
||
end repeat</ |
end repeat</syntaxhighlight> |
||
Line 439: | Line 439: | ||
=={{header|Arc}}== |
=={{header|Arc}}== |
||
< |
<syntaxhighlight lang="arc">(point break |
||
(while t |
(while t |
||
(let x (rand 20) |
(let x (rand 20) |
||
Line 445: | Line 445: | ||
(if (is x 10) |
(if (is x 10) |
||
(break))) |
(break))) |
||
(prn "b: " (rand 20))))</ |
(prn "b: " (rand 20))))</syntaxhighlight> |
||
=={{header|ARM Assembly}}== |
=={{header|ARM Assembly}}== |
||
{{works with|as|Raspberry Pi}} |
{{works with|as|Raspberry Pi}} |
||
<syntaxhighlight lang="arm assembly"> |
|||
<lang ARM Assembly> |
|||
/* ARM assembly Raspberry PI */ |
/* ARM assembly Raspberry PI */ |
||
Line 635: | Line 635: | ||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">while ø [ |
||
a: random 0 19 |
a: random 0 19 |
||
prints [a ""] |
prints [a ""] |
||
Line 647: | Line 647: | ||
print b |
print b |
||
] |
] |
||
print ""</ |
print ""</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 664: | Line 664: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
<syntaxhighlight lang="autohotkey">Loop |
|||
<lang AutoHotkey>Loop |
|||
{ |
{ |
||
Random, var, 0, 19 |
Random, var, 0, 19 |
||
Line 673: | Line 673: | ||
output = %output%`n%var% |
output = %output%`n%var% |
||
} |
} |
||
MsgBox % output</ |
MsgBox % output</syntaxhighlight> |
||
=={{header|Avail}}== |
=={{header|Avail}}== |
||
< |
<syntaxhighlight lang="avail">rng ::= a pRNG; |
||
checked : [0..19]; |
checked : [0..19]; |
||
Do [ |
Do [ |
||
Line 683: | Line 683: | ||
] while checked ≠ 10 alternate with [ |
] while checked ≠ 10 alternate with [ |
||
Print: " " ++ “rng's next [0..19]” ++ "\n"; |
Print: " " ++ “rng's next [0..19]” ++ "\n"; |
||
];</ |
];</syntaxhighlight> |
||
This demonstrates two interesting Avail features: the ''alternate with'' loop structures, which provide two separate code blocks that are run with a check in between, and the random number generator's ability to pick an item from the ranger of a given number type (<code>[0..19]</code> is an expression generating a ''type'' whose values are integers in the range 0-19 inclusive). |
This demonstrates two interesting Avail features: the ''alternate with'' loop structures, which provide two separate code blocks that are run with a check in between, and the random number generator's ability to pick an item from the ranger of a given number type (<code>[0..19]</code> is an expression generating a ''type'' whose values are integers in the range 0-19 inclusive). |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
< |
<syntaxhighlight lang="awk">BEGIN { |
||
for (;;) { |
for (;;) { |
||
print n = int(rand() * 20) |
print n = int(rand() * 20) |
||
Line 695: | Line 695: | ||
print int(rand() * 20) |
print int(rand() * 20) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Axe}}== |
=={{header|Axe}}== |
||
Because Axe only supports breaking out of loops as end conditions, the behavior must be simulated using a return statement. Note, however, that this will exit the current call context, not the necessarily just the current loop. |
Because Axe only supports breaking out of loops as end conditions, the behavior must be simulated using a return statement. Note, however, that this will exit the current call context, not the necessarily just the current loop. |
||
< |
<syntaxhighlight lang="axe">While 1 |
||
rand^20→A |
rand^20→A |
||
Disp A▶Dec |
Disp A▶Dec |
||
Line 706: | Line 706: | ||
rand^20→B |
rand^20→B |
||
Disp B▶Dec,i |
Disp B▶Dec,i |
||
End</ |
End</syntaxhighlight> |
||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
==={{header|Applesoft BASIC}}=== |
==={{header|Applesoft BASIC}}=== |
||
< |
<syntaxhighlight lang="gwbasic">FOR I = 0 TO 1 STEP 0 : N = INT(RND(1) * 20) : PRINT " "N; : IF N <> 10 THEN ? ","INT(RND(1) * 20); : NEXT</syntaxhighlight> |
||
==={{header|BaCon}}=== |
==={{header|BaCon}}=== |
||
< |
<syntaxhighlight lang="freebasic"> |
||
REPEAT |
REPEAT |
||
number = RANDOM(20) |
number = RANDOM(20) |
||
Line 720: | Line 720: | ||
ENDIF |
ENDIF |
||
PRINT "second ",RANDOM(20) |
PRINT "second ",RANDOM(20) |
||
UNTIL FALSE</ |
UNTIL FALSE</syntaxhighlight> |
||
==={{header|BASIC256}}=== |
==={{header|BASIC256}}=== |
||
<syntaxhighlight lang="basic256">do |
|||
<lang BASIC256>do |
|||
i = int(rand * 19) |
i = int(rand * 19) |
||
print i; " "; |
print i; " "; |
||
Line 731: | Line 731: | ||
until false |
until false |
||
print |
print |
||
end</ |
end</syntaxhighlight> |
||
==={{header|Commodore BASIC}}=== |
==={{header|Commodore BASIC}}=== |
||
In Commodore BASIC, the function RND() generates a floating point number from 0.0 to 1.0 (exclusive). |
In Commodore BASIC, the function RND() generates a floating point number from 0.0 to 1.0 (exclusive). |
||
< |
<syntaxhighlight lang="commodorebasic">10 X = RND(-TI) : REM SEED RN GENERATOR |
||
20 A = INT(RND(1)*20) |
20 A = INT(RND(1)*20) |
||
30 PRINT A |
30 PRINT A |
||
Line 742: | Line 742: | ||
60 PRINT B |
60 PRINT B |
||
70 GOTO 20 |
70 GOTO 20 |
||
80 END</ |
80 END</syntaxhighlight> |
||
==={{header|IS-BASIC}}=== |
==={{header|IS-BASIC}}=== |
||
< |
<syntaxhighlight lang="is-basic">100 RANDOMIZE |
||
110 DO |
110 DO |
||
120 LET A=RND(20)+1 |
120 LET A=RND(20)+1 |
||
Line 751: | Line 751: | ||
140 IF A=10 THEN EXIT DO |
140 IF A=10 THEN EXIT DO |
||
150 PRINT RND(20)+1 |
150 PRINT RND(20)+1 |
||
160 LOOP</ |
160 LOOP</syntaxhighlight> |
||
==={{header|QuickBASIC}}=== |
==={{header|QuickBASIC}}=== |
||
{{works with|QuickBasic|4.5}} |
{{works with|QuickBasic|4.5}} |
||
< |
<syntaxhighlight lang="qbasic">do |
||
a = int(rnd * 20) |
a = int(rnd * 20) |
||
print a |
print a |
||
Line 761: | Line 761: | ||
b = int(rnd * 20) |
b = int(rnd * 20) |
||
print b |
print b |
||
loop</ |
loop</syntaxhighlight> |
||
==={{header|True BASIC}}=== |
==={{header|True BASIC}}=== |
||
< |
<syntaxhighlight lang="basic">RANDOMIZE |
||
DO |
DO |
||
Line 777: | Line 777: | ||
LOOP |
LOOP |
||
PRINT |
PRINT |
||
END</ |
END</syntaxhighlight> |
||
Line 783: | Line 783: | ||
On the ZX Spectrum, for loops must be terminated through the NEXT statement, otherwise a memory leak will occur. To terminate a loop prematurely, set the loop counter to the last iterative value and jump to the NEXT statement: |
On the ZX Spectrum, for loops must be terminated through the NEXT statement, otherwise a memory leak will occur. To terminate a loop prematurely, set the loop counter to the last iterative value and jump to the NEXT statement: |
||
< |
<syntaxhighlight lang="zxbasic">10 FOR l = 1 TO 20 |
||
20 IF l = 10 THEN LET l = 20: GO TO 40: REM terminate the loop |
20 IF l = 10 THEN LET l = 20: GO TO 40: REM terminate the loop |
||
30 PRINT l |
30 PRINT l |
||
40 NEXT l |
40 NEXT l |
||
50 STOP</ |
50 STOP</syntaxhighlight> |
||
The correct solution: |
The correct solution: |
||
< |
<syntaxhighlight lang="zxbasic">10 LET a = INT (RND * 20) |
||
20 PRINT a |
20 PRINT a |
||
30 IF a = 10 THEN STOP |
30 IF a = 10 THEN STOP |
||
40 PRINT INT (RND * 20) |
40 PRINT INT (RND * 20) |
||
50 GO TO 10</ |
50 GO TO 10</syntaxhighlight> |
||
=={{header|Batch File}}== |
=={{header|Batch File}}== |
||
< |
<syntaxhighlight lang="dos">@echo off |
||
:loop |
:loop |
||
set /a N=%RANDOM% %% 20 |
set /a N=%RANDOM% %% 20 |
||
Line 805: | Line 805: | ||
set /a N=%RANDOM% %% 20 |
set /a N=%RANDOM% %% 20 |
||
echo %N% |
echo %N% |
||
goto loop</ |
goto loop</syntaxhighlight> |
||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
< |
<syntaxhighlight lang="bbcbasic"> REPEAT |
||
num% = RND(20)-1 |
num% = RND(20)-1 |
||
PRINT num% |
PRINT num% |
||
IF num%=10 THEN EXIT REPEAT |
IF num%=10 THEN EXIT REPEAT |
||
PRINT RND(20)-1 |
PRINT RND(20)-1 |
||
UNTIL FALSE</ |
UNTIL FALSE</syntaxhighlight> |
||
=={{header|bc}}== |
=={{header|bc}}== |
||
< |
<syntaxhighlight lang="bc">s = 1 /* seed of the random number generator */ |
||
scale = 0 |
scale = 0 |
||
Line 839: | Line 839: | ||
r() /* print 2nd number */ |
r() /* print 2nd number */ |
||
} |
} |
||
quit</ |
quit</syntaxhighlight> |
||
=={{header|Befunge}}== |
=={{header|Befunge}}== |
||
<syntaxhighlight lang="befunge"> |
|||
<lang Befunge> |
|||
>60v *2\< |
>60v *2\< |
||
>?>\1-:| |
>?>\1-:| |
||
Line 854: | Line 854: | ||
>^ 7 |
>^ 7 |
||
^ .%++67< |
^ .%++67< |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
<syntaxhighlight lang="c"> |
|||
<lang c> |
|||
int main(){ |
int main(){ |
||
time_t t; |
time_t t; |
||
Line 872: | Line 872: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
Output (example): |
Output (example): |
||
<pre> |
<pre> |
||
Line 888: | Line 888: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang="csharp">class Program |
||
{ |
{ |
||
static void Main(string[] args) |
static void Main(string[] args) |
||
Line 905: | Line 905: | ||
Console.ReadLine(); |
Console.ReadLine(); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <ctime> |
#include <ctime> |
||
#include <cstdlib> |
#include <cstdlib> |
||
Line 923: | Line 923: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Chapel}}== |
=={{header|Chapel}}== |
||
< |
<syntaxhighlight lang="chapel">use Random; |
||
var r = new RandomStream(); |
var r = new RandomStream(); |
||
Line 936: | Line 936: | ||
writeln(b); |
writeln(b); |
||
} |
} |
||
delete r;</ |
delete r;</syntaxhighlight> |
||
=={{header|Chef}}== |
=={{header|Chef}}== |
||
"Liquify" is now depreciated in favor of "Liquefy", but my interpreter/compiler ([http://search.cpan.org/~smueller/Acme-Chef/ Acme::Chef]) works only with "Liquify" so that's how I'm leaving it. At least it'll work no matter which version you use. |
"Liquify" is now depreciated in favor of "Liquefy", but my interpreter/compiler ([http://search.cpan.org/~smueller/Acme-Chef/ Acme::Chef]) works only with "Liquify" so that's how I'm leaving it. At least it'll work no matter which version you use. |
||
<div style='width:full;overflow:scroll'> |
<div style='width:full;overflow:scroll'> |
||
< |
<syntaxhighlight lang="chef">Healthy Vita-Sauce Loop - Broken. |
||
Makes a whole lot of sauce for two people. |
Makes a whole lot of sauce for two people. |
||
Line 998: | Line 998: | ||
Pour contents of the 1st mixing bowl into the 1st baking dish. |
Pour contents of the 1st mixing bowl into the 1st baking dish. |
||
Serves 2.</ |
Serves 2.</syntaxhighlight> |
||
</div> |
</div> |
||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="lisp">(loop [[a b & more] (repeatedly #(rand-int 20))] |
||
(println a) |
(println a) |
||
(when-not (= 10 a) |
(when-not (= 10 a) |
||
(println b) |
(println b) |
||
(recur more)))</ |
(recur more)))</syntaxhighlight> |
||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
{{works with|OpenCOBOL}} |
{{works with|OpenCOBOL}} |
||
< |
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION. |
||
PROGRAM-ID. Random-Nums. |
PROGRAM-ID. Random-Nums. |
||
Line 1,035: | Line 1,035: | ||
COMPUTE Num = FUNCTION REM(FUNCTION RANDOM * 100, 20) |
COMPUTE Num = FUNCTION REM(FUNCTION RANDOM * 100, 20) |
||
DISPLAY Num |
DISPLAY Num |
||
.</ |
.</syntaxhighlight> |
||
=={{header|CoffeeScript}}== |
=={{header|CoffeeScript}}== |
||
We can use print from the Rhino JavaScript shell as in the JavaScript example or console.log, with a result like this: |
We can use print from the Rhino JavaScript shell as in the JavaScript example or console.log, with a result like this: |
||
< |
<syntaxhighlight lang="coffeescript"> |
||
loop |
loop |
||
print a = Math.random() * 20 // 1 |
print a = Math.random() * 20 // 1 |
||
break if a == 10 |
break if a == 10 |
||
print Math.random() * 20 // 1 |
print Math.random() * 20 // 1 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|ColdFusion}}== |
=={{header|ColdFusion}}== |
||
< |
<syntaxhighlight lang="cfm"> |
||
<Cfset randNum = 0> |
<Cfset randNum = 0> |
||
<cfloop condition="randNum neq 10"> |
<cfloop condition="randNum neq 10"> |
||
Line 1,056: | Line 1,056: | ||
<Br> |
<Br> |
||
</cfloop> |
</cfloop> |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
My first two test outputs (I swear this is true) |
My first two test outputs (I swear this is true) |
||
Line 1,124: | Line 1,124: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">(loop for a = (random 20) |
||
do (print a) |
do (print a) |
||
until (= a 10) |
until (= a 10) |
||
do (print (random 20)))</ |
do (print (random 20)))</syntaxhighlight> |
||
=== Using DO === |
=== Using DO === |
||
< |
<syntaxhighlight lang="lisp"> |
||
(do ((a (random 20) (random 20))) ; Initialize to rand and set new rand on every loop |
(do ((a (random 20) (random 20))) ; Initialize to rand and set new rand on every loop |
||
((= a 10) (write a)) ; Break condition and last step |
((= a 10) (write a)) ; Break condition and last step |
||
(format t "~a~3T~a~%" a (random 20))) ; On every loop print formated `a' and rand `b' |
(format t "~a~3T~a~%" a (random 20))) ; On every loop print formated `a' and rand `b' |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,159: | Line 1,159: | ||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.random; |
||
void main() { |
void main() { |
||
Line 1,169: | Line 1,169: | ||
write(uniform(0, 20), " "); |
write(uniform(0, 20), " "); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2 4 9 5 3 7 4 4 14 14 3 7 13 8 13 6 10 </pre> |
<pre>2 4 9 5 3 7 4 4 14 14 3 7 13 8 13 6 10 </pre> |
||
Line 1,175: | Line 1,175: | ||
=={{header|dc}}== |
=={{header|dc}}== |
||
{{trans|bc}} |
{{trans|bc}} |
||
< |
<syntaxhighlight lang="dc">1 ss [s = seed of the random number generator]sz |
||
0k [scale = 0]sz |
0k [scale = 0]sz |
||
Line 1,199: | Line 1,199: | ||
0 0 =r p sz [Print 2nd number.]sz |
0 0 =r p sz [Print 2nd number.]sz |
||
0 0 =B [Continue loop.]sz |
0 0 =B [Continue loop.]sz |
||
]sB 0 0 =B</ |
]sB 0 0 =B</syntaxhighlight> |
||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
< |
<syntaxhighlight lang="delphi">program Project5; |
||
{$APPTYPE CONSOLE} |
{$APPTYPE CONSOLE} |
||
Line 1,219: | Line 1,219: | ||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|DWScript}}== |
=={{header|DWScript}}== |
||
< |
<syntaxhighlight lang="delphi"> |
||
while True do begin |
while True do begin |
||
var num := RandomInt(20); |
var num := RandomInt(20); |
||
PrintLn(num); |
PrintLn(num); |
||
if num=10 then Break; |
if num=10 then Break; |
||
end;</ |
end;</syntaxhighlight> |
||
=={{header|E}}== |
=={{header|E}}== |
||
< |
<syntaxhighlight lang="e">while (true) { |
||
def a := entropy.nextInt(20) |
def a := entropy.nextInt(20) |
||
print(a) |
print(a) |
||
Line 1,239: | Line 1,239: | ||
} |
} |
||
println(" ", entropy.nextInt(20)) |
println(" ", entropy.nextInt(20)) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|EasyLang}}== |
=={{header|EasyLang}}== |
||
<lang>repeat |
<syntaxhighlight lang="text">repeat |
||
a = random 20 |
a = random 20 |
||
print a |
print a |
||
until a = 10 |
until a = 10 |
||
print random 20 |
print random 20 |
||
.</ |
.</syntaxhighlight> |
||
=={{header|Eiffel}}== |
=={{header|Eiffel}}== |
||
< |
<syntaxhighlight lang="eiffel"> |
||
example |
example |
||
-- Eiffel example code |
-- Eiffel example code |
||
Line 1,266: | Line 1,266: | ||
end |
end |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
The output is superfluous and unneeded to read and understand what the Eiffel code is doing. |
The output is superfluous and unneeded to read and understand what the Eiffel code is doing. |
||
Line 1,278: | Line 1,278: | ||
As a result, an output looks almost truly random: |
As a result, an output looks almost truly random: |
||
< |
<syntaxhighlight lang="ela">open datetime random monad io |
||
loop = loop' 1 |
loop = loop' 1 |
||
Line 1,289: | Line 1,289: | ||
loop 10 ::: IO</ |
loop 10 ::: IO</syntaxhighlight> |
||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
{{works with|Elixir|1.2}} |
{{works with|Elixir|1.2}} |
||
< |
<syntaxhighlight lang="elixir">defmodule Loops do |
||
def break, do: break(random) |
def break, do: break(random) |
||
Line 1,305: | Line 1,305: | ||
end |
end |
||
Loops.break</ |
Loops.break</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,327: | Line 1,327: | ||
=={{header|Emacs Lisp}}== |
=={{header|Emacs Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">(defun wait_10 () |
||
(catch 'loop-break |
(catch 'loop-break |
||
(while 't |
(while 't |
||
Line 1,336: | Line 1,336: | ||
(message "current number is: %d" math) ) ) ) ) ) |
(message "current number is: %d" math) ) ) ) ) ) |
||
(wait_10)</ |
(wait_10)</syntaxhighlight> |
||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
< |
<syntaxhighlight lang="erlang">%% Implemented by Arjun Sunel |
||
-module(forever). |
-module(forever). |
||
-export([main/0, for/0]). |
-export([main/0, for/0]). |
||
Line 1,356: | Line 1,356: | ||
for() |
for() |
||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|ERRE}}== |
=={{header|ERRE}}== |
||
<syntaxhighlight lang="erre"> |
|||
<lang ERRE> |
|||
LOOP |
LOOP |
||
A=INT(RND(1)*20) |
A=INT(RND(1)*20) |
||
Line 1,366: | Line 1,366: | ||
PRINT(INT(RND(1)*20)) |
PRINT(INT(RND(1)*20)) |
||
END LOOP |
END LOOP |
||
</syntaxhighlight> |
|||
</lang> |
|||
The <code>RND(X)</code> function returns a random integer from 0 to 1. X is a dummy argument. |
The <code>RND(X)</code> function returns a random integer from 0 to 1. X is a dummy argument. |
||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
< |
<syntaxhighlight lang="euphoria">integer i |
||
while 1 do |
while 1 do |
||
i = rand(20) - 1 |
i = rand(20) - 1 |
||
Line 1,378: | Line 1,378: | ||
end if |
end if |
||
printf(1, "%g ", {rand(20)-1}) |
printf(1, "%g ", {rand(20)-1}) |
||
end while</ |
end while</syntaxhighlight> |
||
The <code>rand()</code> function returns a random integer from 1 to the integer provided. |
The <code>rand()</code> function returns a random integer from 1 to the integer provided. |
||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Loops/Break. Nigel Galloway: February 21st., 2022 |
// Loops/Break. Nigel Galloway: February 21st., 2022 |
||
let n=System.Random() |
let n=System.Random() |
||
let rec fN g=printf "%d " g; if g <> 10 then fN(n.Next(20)) |
let rec fN g=printf "%d " g; if g <> 10 then fN(n.Next(20)) |
||
fN(n.Next(20)) |
fN(n.Next(20)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
Using <code>with-return</code>: |
Using <code>with-return</code>: |
||
< |
<syntaxhighlight lang="factor">[ |
||
[ 20 random [ . ] [ 10 = [ return ] when ] bi 20 random . t ] loop |
[ 20 random [ . ] [ 10 = [ return ] when ] bi 20 random . t ] loop |
||
] with-return</ |
] with-return</syntaxhighlight> |
||
Idiomatic Factor: |
Idiomatic Factor: |
||
< |
<syntaxhighlight lang="factor">[ 20 random [ . ] [ 10 = not ] bi dup [ 20 random . ] when ] loop</syntaxhighlight> |
||
=={{header|Fantom}}== |
=={{header|Fantom}}== |
||
< |
<syntaxhighlight lang="fantom"> |
||
class ForBreak |
class ForBreak |
||
{ |
{ |
||
Line 1,414: | Line 1,414: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
< |
<syntaxhighlight lang="forth">include random.fs |
||
: main |
: main |
||
Line 1,430: | Line 1,430: | ||
10 = if leave then |
10 = if leave then |
||
i random . |
i random . |
||
loop ;</ |
loop ;</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|Fortran|90 and later}} |
{{works with|Fortran|90 and later}} |
||
< |
<syntaxhighlight lang="fortran">program Example |
||
implicit none |
implicit none |
||
Line 1,450: | Line 1,450: | ||
end do |
end do |
||
end program Example</ |
end program Example</syntaxhighlight> |
||
{{works with|Fortran|77 and later}} |
{{works with|Fortran|77 and later}} |
||
< |
<syntaxhighlight lang="fortran"> PROGRAM LOOPBREAK |
||
INTEGER I, RNDINT |
INTEGER I, RNDINT |
||
Line 1,521: | Line 1,521: | ||
ENDIF |
ENDIF |
||
RETURN |
RETURN |
||
END</ |
END</syntaxhighlight> |
||
{{works with|Fortran|66 and earlier}} |
{{works with|Fortran|66 and earlier}} |
||
Line 1,529: | Line 1,529: | ||
The RANDU routine is so notorious that latter-day compilers can supply their own RANDU (using a better method), and further, disregard a user-supplied RANDU routine so it may have to be called RANDUU or some other name! |
The RANDU routine is so notorious that latter-day compilers can supply their own RANDU (using a better method), and further, disregard a user-supplied RANDU routine so it may have to be called RANDUU or some other name! |
||
<syntaxhighlight lang="fortran"> |
|||
<lang Fortran> |
|||
SUBROUTINE RANDU(IX,IY,YFL) |
SUBROUTINE RANDU(IX,IY,YFL) |
||
Copied from the IBM1130 Scientific Subroutines Package (1130-CM-02X): Programmer's Manual, page 60. |
Copied from the IBM1130 Scientific Subroutines Package (1130-CM-02X): Programmer's Manual, page 60. |
||
Line 1,561: | Line 1,561: | ||
20 CONTINUE |
20 CONTINUE |
||
END |
END |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output, converted to along the line: |
Output, converted to along the line: |
||
0 13 4 19 1 7 2 12 4 7 14 11 6 4 0 9 5 12 16 19 18 2 0 13 2 7 10 |
0 13 4 19 1 7 2 12 4 7 14 11 6 4 0 9 5 12 16 19 18 2 0 13 2 7 10 |
||
Line 1,567: | Line 1,567: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
||
Dim i As Integer |
Dim i As Integer |
||
Line 1,582: | Line 1,582: | ||
Print |
Print |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
Sample output |
Sample output |
||
Line 1,593: | Line 1,593: | ||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
< |
<syntaxhighlight lang="frink">while true |
||
{ |
{ |
||
a = random[0,19] |
a = random[0,19] |
||
Line 1,602: | Line 1,602: | ||
b = random[0,19] |
b = random[0,19] |
||
print["$b "] |
print["$b "] |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,609: | Line 1,609: | ||
=={{header|FutureBasic}}== |
=={{header|FutureBasic}}== |
||
< |
<syntaxhighlight lang="futurebasic">include "NSLog.incl" |
||
long num |
long num |
||
Line 1,617: | Line 1,617: | ||
until ( num == 10 ) |
until ( num == 10 ) |
||
HandleEvents</ |
HandleEvents</syntaxhighlight> |
||
=={{header|Gambas}}== |
=={{header|Gambas}}== |
||
< |
<syntaxhighlight lang="gambas">Public Sub Form_Open() |
||
Dim iRand As Integer |
Dim iRand As Integer |
||
Line 1,628: | Line 1,628: | ||
Until iRand = 10 |
Until iRand = 10 |
||
End</ |
End</syntaxhighlight> =={{header|Gambas}}== |
||
'''[https://gambas-playground.proko.eu/?gist=65d2287312298a938e7e8eea8899e38b Click this link to run this code]''' |
'''[https://gambas-playground.proko.eu/?gist=65d2287312298a938e7e8eea8899e38b Click this link to run this code]''' |
||
< |
<syntaxhighlight lang="gambas">Public Sub Main() |
||
Dim byNo As Byte |
Dim byNo As Byte |
||
Line 1,641: | Line 1,641: | ||
Loop |
Loop |
||
End</ |
End</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,648: | Line 1,648: | ||
=={{header|GAP}}== |
=={{header|GAP}}== |
||
< |
<syntaxhighlight lang="gap">while true do |
||
a := Random(0, 19); |
a := Random(0, 19); |
||
Print(a); |
Print(a); |
||
Line 1,664: | Line 1,664: | ||
# 5 10 |
# 5 10 |
||
# 1 16 |
# 1 16 |
||
# 10</ |
# 10</syntaxhighlight> |
||
=={{header|GML}}== |
=={{header|GML}}== |
||
< |
<syntaxhighlight lang="gml">while(1) |
||
{ |
{ |
||
a = floor(random(19)) |
a = floor(random(19)) |
||
Line 1,676: | Line 1,676: | ||
show_message(string(a)) |
show_message(string(a)) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 1,696: | Line 1,696: | ||
fmt.Println(b) |
fmt.Println(b) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
< |
<syntaxhighlight lang="groovy">final random = new Random() |
||
while (true) { |
while (true) { |
||
Line 1,707: | Line 1,707: | ||
print ' ' |
print ' ' |
||
println random.nextInt(20) |
println random.nextInt(20) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|GW-BASIC}}== |
=={{header|GW-BASIC}}== |
||
< |
<syntaxhighlight lang="qbasic">10 NUM = 0 |
||
20 WHILE NUM <> 10 |
20 WHILE NUM <> 10 |
||
30 NUM = INT(RND * 20) |
30 NUM = INT(RND * 20) |
||
40 PRINT NUM |
40 PRINT NUM |
||
50 WEND</ |
50 WEND</syntaxhighlight> |
||
=={{header|Harbour}}== |
=={{header|Harbour}}== |
||
< |
<syntaxhighlight lang="visualfoxpro">PROCEDURE Loop() |
||
LOCAL n |
LOCAL n |
||
Line 1,729: | Line 1,729: | ||
ENDDO |
ENDDO |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Control.Monad |
||
import System.Random |
import System.Random |
||
Line 1,740: | Line 1,740: | ||
unless (r==k) $ do |
unless (r==k) $ do |
||
print =<< randomRIO (0,n) |
print =<< randomRIO (0,n) |
||
loopBreak n k</ |
loopBreak n k</syntaxhighlight> |
||
Use: |
Use: |
||
<lang |
<syntaxhighlight lang="haskell">loopBreak 19 10</syntaxhighlight> |
||
=={{header|Haxe}}== |
=={{header|Haxe}}== |
||
< |
<syntaxhighlight lang="haxe">class Program { |
||
static public function main():Void { |
static public function main():Void { |
||
while(true) { |
while(true) { |
||
Line 1,756: | Line 1,756: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|hexiscript}}== |
=={{header|hexiscript}}== |
||
< |
<syntaxhighlight lang="hexiscript">while true |
||
let r rand 20 |
let r rand 20 |
||
println r |
println r |
||
Line 1,766: | Line 1,766: | ||
endif |
endif |
||
println rand 20 |
println rand 20 |
||
endwhile</ |
endwhile</syntaxhighlight> |
||
=={{header|HicEst}}== |
=={{header|HicEst}}== |
||
< |
<syntaxhighlight lang="hicest">1 DO i = 1, 1E20 ! "forever" |
||
a = INT( RAN(10, 10) ) |
a = INT( RAN(10, 10) ) |
||
WRITE(name) a |
WRITE(name) a |
||
Line 1,777: | Line 1,777: | ||
ENDDO |
ENDDO |
||
10 |
10 |
||
END</ |
END</syntaxhighlight> |
||
=={{header|HolyC}}== |
=={{header|HolyC}}== |
||
< |
<syntaxhighlight lang="holyc">U16 a, b; |
||
while (1) { |
while (1) { |
||
a = RandU16 % 20; |
a = RandU16 % 20; |
||
Line 1,791: | Line 1,791: | ||
Print("%d\n", b); |
Print("%d\n", b); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
< |
<syntaxhighlight lang="icon">procedure main() |
||
while 10 ~= writes(?20-1) do write(", ",?20-1) |
while 10 ~= writes(?20-1) do write(", ",?20-1) |
||
end</ |
end</syntaxhighlight> |
||
Notes: |
Notes: |
||
* For any positive integer i, ?i produces a value j where 1 <= j <= i |
* For any positive integer i, ?i produces a value j where 1 <= j <= i |
||
* Although this can be written with a break (e.g. repeat expression & break), there is no need to actually use one. (And it's ugly). |
* Although this can be written with a break (e.g. repeat expression & break), there is no need to actually use one. (And it's ugly). |
||
* Programmers new to Icon/Unicon need to understand that just about everything returns values including comparison operators, I/O functions like write/writes. |
* Programmers new to Icon/Unicon need to understand that just about everything returns values including comparison operators, I/O functions like write/writes. |
||
* This program will perform similarly but not identically under Icon and Unicon because the random operator ?i behaves differently. While both produce pseudo-random numbers a different generator is used. Also, the sequence produced by Icon begins with the same seed value and is repeatable whereas the sequence produced by Unicon does not. One way to force Icon to use different random sequences on each call would be to add the line < |
* This program will perform similarly but not identically under Icon and Unicon because the random operator ?i behaves differently. While both produce pseudo-random numbers a different generator is used. Also, the sequence produced by Icon begins with the same seed value and is repeatable whereas the sequence produced by Unicon does not. One way to force Icon to use different random sequences on each call would be to add the line <syntaxhighlight lang="icon">&random := integer(map("smhSMH","Hh:Mm:Ss",&clock))</syntaxhighlight> at the start of the <tt>main</tt> procedure to set the random number seed based on the time of day. |
||
=={{header|Io}}== |
=={{header|Io}}== |
||
< |
<syntaxhighlight lang="io">loop( |
||
a := Random value(0,20) floor |
a := Random value(0,20) floor |
||
write(a) |
write(a) |
||
Line 1,810: | Line 1,810: | ||
b := Random value(0,20) floor |
b := Random value(0,20) floor |
||
writeln(" ",b) |
writeln(" ",b) |
||
)</ |
)</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
< |
<syntaxhighlight lang="j">loopexample=: {{ |
||
while. do. |
while. do. |
||
echo k=. ?20 |
echo k=. ?20 |
||
Line 1,819: | Line 1,819: | ||
echo ?20 |
echo ?20 |
||
end. |
end. |
||
}}</ |
}}</syntaxhighlight> |
||
Note that <code>break.</code> or <code>goto_FOO.</code> could have been used in place of <code>return.</code>: |
Note that <code>break.</code> or <code>goto_FOO.</code> could have been used in place of <code>return.</code>: |
||
< |
<syntaxhighlight lang="j">loopexample2=: verb define |
||
while. do. |
while. do. |
||
echo k=. ?20 |
echo k=. ?20 |
||
Line 1,829: | Line 1,829: | ||
echo ?20 |
echo ?20 |
||
end. |
end. |
||
)</ |
)</syntaxhighlight> |
||
< |
<syntaxhighlight lang="j">loopexample3=: {{ |
||
while. do. |
while. do. |
||
echo k=. ?20 |
echo k=. ?20 |
||
Line 1,838: | Line 1,838: | ||
end. |
end. |
||
label_done. |
label_done. |
||
}}</ |
}}</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java">import java.util.Random; |
||
Random rand = new Random(); |
Random rand = new Random(); |
||
Line 1,850: | Line 1,850: | ||
int b = rand.nextInt(20); |
int b = rand.nextInt(20); |
||
System.out.println(b); |
System.out.println(b); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
< |
<syntaxhighlight lang="javascript">for (;;) { |
||
var a = Math.floor(Math.random() * 20); |
var a = Math.floor(Math.random() * 20); |
||
print(a); |
print(a); |
||
Line 1,860: | Line 1,860: | ||
a = Math.floor(Math.random() * 20); |
a = Math.floor(Math.random() * 20); |
||
print(a); |
print(a); |
||
}</ |
}</syntaxhighlight> |
||
The <code>print()</code> function is available in the [[Rhino]] JavaScript shell. |
The <code>print()</code> function is available in the [[Rhino]] JavaScript shell. |
||
Line 1,868: | Line 1,868: | ||
In a functional idiom of JavaScript, we might instead write something like: |
In a functional idiom of JavaScript, we might instead write something like: |
||
< |
<syntaxhighlight lang="javascript">(function streamTillInitialTen() { |
||
var nFirst = Math.floor(Math.random() * 20); |
var nFirst = Math.floor(Math.random() * 20); |
||
Line 1,880: | Line 1,880: | ||
return streamTillInitialTen(); |
return streamTillInitialTen(); |
||
})();</ |
})();</syntaxhighlight> |
||
Obtaining runs like: |
Obtaining runs like: |
||
Line 1,921: | Line 1,921: | ||
Though returning a value composes better, and costs less IO traffic, than firing off side-effects from a moving thread: |
Though returning a value composes better, and costs less IO traffic, than firing off side-effects from a moving thread: |
||
< |
<syntaxhighlight lang="javascript">console.log( |
||
(function streamTillInitialTen() { |
(function streamTillInitialTen() { |
||
var nFirst = Math.floor(Math.random() * 20); |
var nFirst = Math.floor(Math.random() * 20); |
||
Line 1,934: | Line 1,934: | ||
); |
); |
||
})().join('\n') |
})().join('\n') |
||
);</ |
);</syntaxhighlight> |
||
Sample result: |
Sample result: |
||
Line 1,962: | Line 1,962: | ||
Currently, jq does not have a built-in random-number generator, so here we borrow one of the linear congruential generators defined at https://rosettacode.org/wiki/Linear_congruential_generator - |
Currently, jq does not have a built-in random-number generator, so here we borrow one of the linear congruential generators defined at https://rosettacode.org/wiki/Linear_congruential_generator - |
||
< |
<syntaxhighlight lang="jq"># 15-bit integers generated using the same formula as rand() |
||
# from the Microsoft C Runtime. |
# from the Microsoft C Runtime. |
||
# Input: [ count, state, rand ] |
# Input: [ count, state, rand ] |
||
Line 1,977: | Line 1,977: | ||
# Generate random integers from 0 to (n-1): |
# Generate random integers from 0 to (n-1): |
||
def rand(n): n * (rand_Microsoft(17) / 32768) | trunc;</ |
def rand(n): n * (rand_Microsoft(17) / 32768) | trunc;</syntaxhighlight> |
||
'''"take"''' |
'''"take"''' |
||
< |
<syntaxhighlight lang="jq">def take(s; cond): |
||
label $done |
label $done |
||
| foreach s as $n (null; $n; if $n | cond | not then break $done else . end);</ |
| foreach s as $n (null; $n; if $n | cond | not then break $done else . end);</syntaxhighlight> |
||
'''"count"''' |
'''"count"''' |
||
Since the PRNG used here is deterministic, we'll just count the number of integers generated: |
Since the PRNG used here is deterministic, we'll just count the number of integers generated: |
||
< |
<syntaxhighlight lang="jq">def count(s): reduce s as $i (0; . + 1);</syntaxhighlight> |
||
'''Example''' |
'''Example''' |
||
Line 1,996: | Line 1,996: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
<syntaxhighlight lang="julia"> |
|||
<lang Julia> |
|||
while true |
while true |
||
n = rand(0:19) |
n = rand(0:19) |
||
Line 2,007: | Line 2,007: | ||
@printf "%4d\n" n |
@printf "%4d\n" n |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,027: | Line 2,027: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="scala">import java.util.Random |
||
fun main(args: Array<String>) { |
fun main(args: Array<String>) { |
||
Line 2,037: | Line 2,037: | ||
println(rand.nextInt(20)) |
println(rand.nextInt(20)) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Lambdatalk}}== |
=={{header|Lambdatalk}}== |
||
Line 2,050: | Line 2,050: | ||
{loops_break 0} |
{loops_break 0} |
||
-> 0 16 8 5 9 17 9 18 1 18 1 1 12 13 15 1 10 -> end of loop |
-> 0 16 8 5 9 17 9 18 1 18 1 1 12 13 15 1 10 -> end of loop |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Lang5}}== |
=={{header|Lang5}}== |
||
< |
<syntaxhighlight lang="lang5">do 20 ? int dup . 10 == if break then 20 ? int . loop</syntaxhighlight> |
||
=={{header|langur}}== |
=={{header|langur}}== |
||
< |
<syntaxhighlight lang="langur">for { |
||
val .i = random 0..19 |
val .i = random 0..19 |
||
write .i, " " |
write .i, " " |
||
if .i == 10 { writeln(); break } |
if .i == 10 { writeln(); break } |
||
write random(0..19), " " |
write random(0..19), " " |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,067: | Line 2,067: | ||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
< |
<syntaxhighlight lang="lasso">local(x = 0) |
||
while(#x != 10) => {^ |
while(#x != 10) => {^ |
||
#x = integer_random(19,0) |
#x = integer_random(19,0) |
||
Line 2,073: | Line 2,073: | ||
#x == 10 ? loop_abort |
#x == 10 ? loop_abort |
||
', '+integer_random(19,0)+'\r' |
', '+integer_random(19,0)+'\r' |
||
^}</ |
^}</syntaxhighlight> |
||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
The task specifies a "number". |
The task specifies a "number". |
||
< |
<syntaxhighlight lang="lb">while num<>10 |
||
num=rnd(1)*20 |
num=rnd(1)*20 |
||
print num |
print num |
||
Line 2,083: | Line 2,083: | ||
print rnd(1)*20 |
print rnd(1)*20 |
||
wend |
wend |
||
</ |
</syntaxhighlight>If "integer" was meant, this code fulfils that requirement. |
||
< |
<syntaxhighlight lang="lb">while num<>10 |
||
num=int(rnd(1)*20) |
num=int(rnd(1)*20) |
||
print num |
print num |
||
Line 2,090: | Line 2,090: | ||
print int(rnd(1)*20) |
print int(rnd(1)*20) |
||
wend |
wend |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Lingo}}== |
=={{header|Lingo}}== |
||
< |
<syntaxhighlight lang="lingo">repeat while TRUE |
||
n = random(20)-1 |
n = random(20)-1 |
||
put n |
put n |
||
if n = 10 then exit repeat |
if n = 10 then exit repeat |
||
put random(20)-1 |
put random(20)-1 |
||
end repeat</ |
end repeat</syntaxhighlight> |
||
=={{header|Lisaac}}== |
=={{header|Lisaac}}== |
||
< |
<syntaxhighlight lang="lisaac">Section Header |
||
+ name := TEST_LOOP_BREAK; |
+ name := TEST_LOOP_BREAK; |
||
Line 2,121: | Line 2,121: | ||
'\n'.print; |
'\n'.print; |
||
} |
} |
||
);</ |
);</syntaxhighlight> |
||
=={{header|LiveCode}}== |
=={{header|LiveCode}}== |
||
< |
<syntaxhighlight lang="livecode">command loopForeverRandom |
||
repeat forever |
repeat forever |
||
put random(20) - 1 into tRand |
put random(20) - 1 into tRand |
||
Line 2,132: | Line 2,132: | ||
end repeat |
end repeat |
||
end loopForeverRandom |
end loopForeverRandom |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">repeat |
||
k = math.random(19) |
k = math.random(19) |
||
print(k) |
print(k) |
||
if k == 10 then break end |
if k == 10 then break end |
||
print(math.random(19) |
print(math.random(19) |
||
until false</ |
until false</syntaxhighlight> |
||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
We use block of module to loop. Break also can be used, but breaks nested blocks (without crossing modules/functions). Using break in second Checkit module we break three blocks. |
We use block of module to loop. Break also can be used, but breaks nested blocks (without crossing modules/functions). Using break in second Checkit module we break three blocks. |
||
<syntaxhighlight lang="m2000 interpreter"> |
|||
<lang M2000 Interpreter> |
|||
Module Checkit { |
Module Checkit { |
||
M=Random(0, 19) |
M=Random(0, 19) |
||
Line 2,171: | Line 2,171: | ||
} |
} |
||
Checkit |
Checkit |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|M4}}== |
=={{header|M4}}== |
||
< |
<syntaxhighlight lang="m4">define(`randSeed',141592653)dnl |
||
define(`setRand', |
define(`setRand', |
||
`define(`randSeed',ifelse(eval($1<10000),1,`eval(20000-$1)',`$1'))')dnl |
`define(`randSeed',ifelse(eval($1<10000),1,`eval(20000-$1)',`$1'))')dnl |
||
Line 2,185: | Line 2,185: | ||
loopbreak')')dnl |
loopbreak')')dnl |
||
dnl |
dnl |
||
loopbreak</ |
loopbreak</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,197: | Line 2,197: | ||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">r := rand( 0 .. 19 ): |
||
do |
do |
||
n := r(); |
n := r(); |
||
Line 2,205: | Line 2,205: | ||
end if; |
end if; |
||
printf( "%d\n", r() ); |
printf( "%d\n", r() ); |
||
end do:</ |
end do:</syntaxhighlight> |
||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">While[(Print[#];#!=10)&[RandomIntger[{0,19}]], |
||
Print[RandomInteger[{0,19}] |
Print[RandomInteger[{0,19}] |
||
]</ |
]</syntaxhighlight> |
||
=={{header|Maxima}}== |
=={{header|Maxima}}== |
||
< |
<syntaxhighlight lang="maxima">/* To exit the innermost block, use return(<value>) */ |
||
block([n], |
block([n], |
||
Line 2,248: | Line 2,248: | ||
> x: 2; |
> x: 2; |
||
> exit; |
> exit; |
||
2</ |
2</syntaxhighlight> |
||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
<syntaxhighlight lang="maxscript"> |
|||
<lang MAXScript> |
|||
while true do |
while true do |
||
( |
( |
||
Line 2,260: | Line 2,260: | ||
format ("B: % \n") b |
format ("B: % \n") b |
||
) |
) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|min}}== |
=={{header|min}}== |
||
{{works with|min|0.19.6}} |
{{works with|min|0.19.6}} |
||
< |
<syntaxhighlight lang="min">randomize |
||
(19 random puts 10 ==) (19 random puts!) () () linrec</ |
(19 random puts 10 ==) (19 random puts!) () () linrec</syntaxhighlight> |
||
=={{header|МК-61/52}}== |
=={{header|МК-61/52}}== |
||
<lang>СЧ 2 0 * П0 |
<syntaxhighlight lang="text">СЧ 2 0 * П0 |
||
1 0 - [x] x#0 18 |
1 0 - [x] x#0 18 |
||
СЧ 2 0 * П1 |
СЧ 2 0 * П1 |
||
БП 00 ИП0 С/П</ |
БП 00 ИП0 С/П</syntaxhighlight> |
||
=={{header|Modula-3}}== |
=={{header|Modula-3}}== |
||
< |
<syntaxhighlight lang="modula3">MODULE Break EXPORTS Main; |
||
IMPORT IO, Fmt, Random; |
IMPORT IO, Fmt, Random; |
||
Line 2,290: | Line 2,290: | ||
END; |
END; |
||
END; |
END; |
||
END Break.</ |
END Break.</syntaxhighlight> |
||
=={{header|MOO}}== |
=={{header|MOO}}== |
||
< |
<syntaxhighlight lang="moo">while (1) |
||
a = random(20) - 1; |
a = random(20) - 1; |
||
player:tell(a); |
player:tell(a); |
||
Line 2,301: | Line 2,301: | ||
b = random(20) - 1; |
b = random(20) - 1; |
||
player:tell(b); |
player:tell(b); |
||
endwhile</ |
endwhile</syntaxhighlight> |
||
=={{header|MUMPS}}== |
=={{header|MUMPS}}== |
||
< |
<syntaxhighlight lang="mumps">BREAKLOOP |
||
NEW A,B |
NEW A,B |
||
SET A="" |
SET A="" |
||
Line 2,318: | Line 2,318: | ||
NEW A,B |
NEW A,B |
||
FOR SET A=$RANDOM(20) WRITE !,A QUIT:A=10 SET B=$RANDOM(20) WRITE ?6,B |
FOR SET A=$RANDOM(20) WRITE !,A QUIT:A=10 SET B=$RANDOM(20) WRITE ?6,B |
||
KILL A,B QUIT</ |
KILL A,B QUIT</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>USER>D BREAKLOOP^ROSETTA |
<pre>USER>D BREAKLOOP^ROSETTA |
||
Line 2,343: | Line 2,343: | ||
=={{header|Neko}}== |
=={{header|Neko}}== |
||
<syntaxhighlight lang="actionscript">/** |
|||
<lang ActionScript>/** |
|||
Loops/Break in Neko |
Loops/Break in Neko |
||
Tectonics: |
Tectonics: |
||
Line 2,364: | Line 2,364: | ||
$print(r, " "); |
$print(r, " "); |
||
} |
} |
||
$print("\n");</ |
$print("\n");</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,373: | Line 2,373: | ||
=={{header|Nemerle}}== |
=={{header|Nemerle}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
< |
<syntaxhighlight lang="nemerle">using System; |
||
using System.Console; |
using System.Console; |
||
using Nemerle.Imperative; |
using Nemerle.Imperative; |
||
Line 2,391: | Line 2,391: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref savelog symbols nobinary |
options replace format comments java crossref savelog symbols nobinary |
||
Line 2,410: | Line 2,410: | ||
end lb |
end lb |
||
say |
say |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|NewLISP}}== |
=={{header|NewLISP}}== |
||
< |
<syntaxhighlight lang="newlisp">(until (= 10 (println (rand 20))) |
||
(println (rand 20)))</ |
(println (rand 20)))</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="nim">import random |
||
while true: |
while true: |
||
Line 2,426: | Line 2,426: | ||
break |
break |
||
let b = random(19) |
let b = random(19) |
||
echo b</ |
echo b</syntaxhighlight> |
||
=={{header|NS-HUBASIC}}== |
=={{header|NS-HUBASIC}}== |
||
< |
<syntaxhighlight lang="ns-hubasic">10 I=RND(20) |
||
20 PRINT I |
20 PRINT I |
||
30 IF I=10 THEN STOP |
30 IF I=10 THEN STOP |
||
40 PRINT RND(20) |
40 PRINT RND(20) |
||
50 GOTO 10</ |
50 GOTO 10</syntaxhighlight> |
||
=={{header|Oberon-2}}== |
=={{header|Oberon-2}}== |
||
Works with oo2c Version 2 |
Works with oo2c Version 2 |
||
< |
<syntaxhighlight lang="oberon2"> |
||
MODULE LoopBreak; |
MODULE LoopBreak; |
||
IMPORT |
IMPORT |
||
Line 2,459: | Line 2,459: | ||
Do |
Do |
||
END LoopBreak. |
END LoopBreak. |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
< |
<syntaxhighlight lang="objeck"> |
||
while(true) { |
while(true) { |
||
a := (Float->Random() * 20.0)->As(Int); |
a := (Float->Random() * 20.0)->As(Int); |
||
Line 2,472: | Line 2,472: | ||
a->PrintLine(); |
a->PrintLine(); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang="ocaml"># Random.self_init();; |
||
- : unit = () |
- : unit = () |
||
Line 2,492: | Line 2,492: | ||
13 |
13 |
||
10 |
10 |
||
Exception: Pervasives.Exit.</ |
Exception: Pervasives.Exit.</syntaxhighlight> |
||
=={{header|Octave}}== |
=={{header|Octave}}== |
||
< |
<syntaxhighlight lang="octave">while(1) |
||
a = floor(unifrnd(0,20, 1)); |
a = floor(unifrnd(0,20, 1)); |
||
disp(a) |
disp(a) |
||
Line 2,503: | Line 2,503: | ||
b = floor(unifrnd(0,20, 1)); |
b = floor(unifrnd(0,20, 1)); |
||
disp(b) |
disp(b) |
||
endwhile</ |
endwhile</syntaxhighlight> |
||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
< |
<syntaxhighlight lang="oforth">while(true) [ |
||
19 rand dup print ":" print |
19 rand dup print ":" print |
||
10 == ifTrue: [ break ] |
10 == ifTrue: [ break ] |
||
19 rand print " " print |
19 rand print " " print |
||
]</ |
]</syntaxhighlight> |
||
=={{header|Ol}}== |
=={{header|Ol}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
(import (otus random!)) |
(import (otus random!)) |
||
Line 2,522: | Line 2,522: | ||
(print (rand! 20)) |
(print (rand! 20)) |
||
(loop)))) |
(loop)))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|ooRexx}}== |
=={{header|ooRexx}}== |
||
< |
<syntaxhighlight lang="oorexx">/*REXX **************************************************************** |
||
* Three Ways to leave a Loop |
* Three Ways to leave a Loop |
||
* ooRexx added the possibility to leave an outer loop |
* ooRexx added the possibility to leave an outer loop |
||
Line 2,569: | Line 2,569: | ||
End |
End |
||
Else |
Else |
||
Say 'Leave label-name is probably not supported in' v</ |
Say 'Leave label-name is probably not supported in' v</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>i1=1 |
<pre>i1=1 |
||
Line 2,596: | Line 2,596: | ||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
We can implement this either with recursion or with a special type of the for-loop. Both can be considered idiomatic. |
We can implement this either with recursion or with a special type of the for-loop. Both can be considered idiomatic. |
||
< |
<syntaxhighlight lang="oz">for break:Break do |
||
R = {OS.rand} mod 20 |
R = {OS.rand} mod 20 |
||
in |
in |
||
Line 2,603: | Line 2,603: | ||
else {Show {OS.rand} mod 20} |
else {Show {OS.rand} mod 20} |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
< |
<syntaxhighlight lang="parigp">while(1, |
||
t=random(20); |
t=random(20); |
||
print(t); |
print(t); |
||
if(t==10, break); |
if(t==10, break); |
||
print(random(20)) |
print(random(20)) |
||
)</ |
)</syntaxhighlight> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
Line 2,617: | Line 2,617: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">while (1) { |
||
my $a = int(rand(20)); |
my $a = int(rand(20)); |
||
print "$a\n"; |
print "$a\n"; |
||
Line 2,625: | Line 2,625: | ||
my $b = int(rand(20)); |
my $b = int(rand(20)); |
||
print "$b\n"; |
print "$b\n"; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 2,632: | Line 2,632: | ||
The rand() function returns a random integer from 1 to the integer provided. |
The rand() function returns a random integer from 1 to the integer provided. |
||
<!--< |
<!--<syntaxhighlight lang="phix">--> |
||
<span style="color: #004080;">integer</span> <span style="color: #000000;">i</span> |
<span style="color: #004080;">integer</span> <span style="color: #000000;">i</span> |
||
<span style="color: #008080;">while</span> <span style="color: #000000;">1</span> <span style="color: #008080;">do</span> |
<span style="color: #008080;">while</span> <span style="color: #000000;">1</span> <span style="color: #008080;">do</span> |
||
Line 2,640: | Line 2,640: | ||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"%g\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #000000;">20</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span> |
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"%g\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #000000;">20</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
Line 2,651: | Line 2,651: | ||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
< |
<syntaxhighlight lang="php">while (true) { |
||
$a = rand(0,19); |
$a = rand(0,19); |
||
echo "$a\n"; |
echo "$a\n"; |
||
Line 2,658: | Line 2,658: | ||
$b = rand(0,19); |
$b = rand(0,19); |
||
echo "$b\n"; |
echo "$b\n"; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
Literally: |
Literally: |
||
< |
<syntaxhighlight lang="picolisp">(use R |
||
(loop |
(loop |
||
(println (setq R (rand 1 19))) |
(println (setq R (rand 1 19))) |
||
(T (= 10 R)) |
(T (= 10 R)) |
||
(println (rand 1 19)) ) )</ |
(println (rand 1 19)) ) )</syntaxhighlight> |
||
Shorter: |
Shorter: |
||
< |
<syntaxhighlight lang="picolisp">(until (= 10 (println (rand 1 19))) |
||
(println (rand 1 19)) )</ |
(println (rand 1 19)) )</syntaxhighlight> |
||
=={{header|Pike}}== |
=={{header|Pike}}== |
||
< |
<syntaxhighlight lang="pike">int main(){ |
||
while(1){ |
while(1){ |
||
int a = random(20); |
int a = random(20); |
||
Line 2,682: | Line 2,682: | ||
write(b + "\n"); |
write(b + "\n"); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
<syntaxhighlight lang="pl/i"> |
|||
<lang PL/I> |
|||
do forever; |
do forever; |
||
k = trunc(random()*20); |
k = trunc(random()*20); |
||
Line 2,693: | Line 2,693: | ||
put skip list (k); |
put skip list (k); |
||
end; |
end; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Plain English}}== |
=={{header|Plain English}}== |
||
< |
<syntaxhighlight lang="plainenglish">To run: |
||
Start up. |
Start up. |
||
Demonstrate breaking. |
Demonstrate breaking. |
||
Line 2,712: | Line 2,712: | ||
To write a number to the console: |
To write a number to the console: |
||
Convert the number to a string. |
Convert the number to a string. |
||
Write the string to the console.</ |
Write the string to the console.</syntaxhighlight> |
||
=={{header|PostScript}}== |
=={{header|PostScript}}== |
||
< |
<syntaxhighlight lang="postscript">realtime srand % init RNG |
||
{ |
{ |
||
rand 20 mod % generate number between 0 and 19 |
rand 20 mod % generate number between 0 and 19 |
||
dup = % print it |
dup = % print it |
||
10 eq { exit } if % exit if 10 |
10 eq { exit } if % exit if 10 |
||
} loop</ |
} loop</syntaxhighlight> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
< |
<syntaxhighlight lang="powershell">$r = New-Object Random |
||
for () { |
for () { |
||
$n = $r.Next(20) |
$n = $r.Next(20) |
||
Line 2,731: | Line 2,731: | ||
} |
} |
||
Write-Host $r.Next(20) |
Write-Host $r.Next(20) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">If OpenConsole() |
||
Repeat |
Repeat |
||
Line 2,750: | Line 2,750: | ||
Input() |
Input() |
||
CloseConsole() |
CloseConsole() |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">from random import randrange |
||
while True: |
while True: |
||
Line 2,761: | Line 2,761: | ||
break |
break |
||
b = randrange(20) |
b = randrange(20) |
||
print(b)</ |
print(b)</syntaxhighlight> |
||
=={{header|QB64}}== |
=={{header|QB64}}== |
||
''CBTJD'': 2020/03/14 |
''CBTJD'': 2020/03/14 |
||
< |
<syntaxhighlight lang="qbasic">RANDOMIZE TIMER |
||
DO |
DO |
||
n = INT(RND * 20) |
n = INT(RND * 20) |
||
Line 2,775: | Line 2,775: | ||
PRINT n |
PRINT n |
||
END IF |
END IF |
||
LOOP UNTIL 0</ |
LOOP UNTIL 0</syntaxhighlight> |
||
=={{header|Qi}}== |
=={{header|Qi}}== |
||
<syntaxhighlight lang="qi"> |
|||
<lang qi> |
|||
(define loop -> (if (= 10 (PRINT (random 20))) |
(define loop -> (if (= 10 (PRINT (random 20))) |
||
true |
true |
||
Line 2,784: | Line 2,784: | ||
(loop)))) |
(loop)))) |
||
(loop) |
(loop) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
< |
<syntaxhighlight lang="quackery">[ 20 random |
||
dup echo sp |
dup echo sp |
||
10 = if done |
10 = if done |
||
20 random echo cr |
20 random echo cr |
||
again ]</ |
again ]</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 2,808: | Line 2,808: | ||
=={{header|R}}== |
=={{header|R}}== |
||
{{works with|R|2.8.1}} |
{{works with|R|2.8.1}} |
||
< |
<syntaxhighlight lang="r">sample0to19 <- function() sample(0L:19L, 1,replace=TRUE) |
||
repeat |
repeat |
||
{ |
{ |
||
Line 2,819: | Line 2,819: | ||
result2 <- sample0to19() |
result2 <- sample0to19() |
||
cat(result1, result2, "\n") |
cat(result1, result2, "\n") |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
(let loop () |
(let loop () |
||
Line 2,831: | Line 2,831: | ||
(displayln (random 20)) |
(displayln (random 20)) |
||
(loop))) |
(loop))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
{{works with|Rakudo|#21 "Seattle"}} |
{{works with|Rakudo|#21 "Seattle"}} |
||
<lang |
<syntaxhighlight lang="raku" line>loop { |
||
say my $n = (0..19).pick; |
say my $n = (0..19).pick; |
||
last if $n == 10; |
last if $n == 10; |
||
say (0..19).pick; |
say (0..19).pick; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|REBOL}}== |
=={{header|REBOL}}== |
||
< |
<syntaxhighlight lang="rebol">REBOL [ |
||
Title: "Loop/Break" |
Title: "Loop/Break" |
||
URL: http://rosettacode.org/wiki/Loop/Break |
URL: http://rosettacode.org/wiki/Loop/Break |
||
Line 2,858: | Line 2,858: | ||
print rejoin [" " r20] |
print rejoin [" " r20] |
||
] |
] |
||
print ""</ |
print ""</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,870: | Line 2,870: | ||
=={{header|Red}}== |
=={{header|Red}}== |
||
{{trans|REBOL}} |
{{trans|REBOL}} |
||
< |
<syntaxhighlight lang="rebol">Red [ |
||
Title: "Loops/Break" |
Title: "Loops/Break" |
||
URL: http://rosettacode.org/wiki/Loops/Break |
URL: http://rosettacode.org/wiki/Loops/Break |
||
Line 2,884: | Line 2,884: | ||
print rejoin [" " r20] |
print rejoin [" " r20] |
||
] |
] |
||
print ""</ |
print ""</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,896: | Line 2,896: | ||
=={{header|Retro}}== |
=={{header|Retro}}== |
||
< |
<syntaxhighlight lang="retro">doc{ |
||
A couple of helper functions to make the rest of the |
A couple of helper functions to make the rest of the |
||
code more readable. |
code more readable. |
||
Line 2,918: | Line 2,918: | ||
[ rand dup . 10 <> [ [ rand . ] ifTrue ] sip ] while |
[ rand dup . 10 <> [ [ rand . ] ifTrue ] sip ] while |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program demonstrates a FOREVER DO loop with a test to LEAVE (break). */ |
||
/*REXX's RANDOM BIF returns an integer.*/ |
/*REXX's RANDOM BIF returns an integer.*/ |
||
do forever /*perform loop until da cows come home.*/ |
do forever /*perform loop until da cows come home.*/ |
||
Line 2,930: | Line 2,930: | ||
say right(b, 5) /*show B right─justified, column 2.*/ |
say right(b, 5) /*show B right─justified, column 2.*/ |
||
end /*forever*/ /* [↑] CHAROUT , xxx writes to term.*/ |
end /*forever*/ /* [↑] CHAROUT , xxx writes to term.*/ |
||
/*stick a fork in it, we're all done. */</ |
/*stick a fork in it, we're all done. */</syntaxhighlight> |
||
{{out|output}} |
{{out|output}} |
||
Line 2,989: | Line 2,989: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
while true |
while true |
||
a = random(20) |
a = random(20) |
||
Line 2,995: | Line 2,995: | ||
if a = 10 exit ok |
if a = 10 exit ok |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">loop do |
||
a = rand(20) |
a = rand(20) |
||
print a |
print a |
||
Line 3,007: | Line 3,007: | ||
b = rand(20) |
b = rand(20) |
||
puts "\t#{b}" |
puts "\t#{b}" |
||
end</ |
end</syntaxhighlight> |
||
or |
or |
||
< |
<syntaxhighlight lang="ruby">loop do |
||
print a = rand(20) |
print a = rand(20) |
||
puts or break if a == 10 |
puts or break if a == 10 |
||
puts "\t#{rand(20)}" |
puts "\t#{rand(20)}" |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,032: | Line 3,032: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
{{libheader|rand}} |
{{libheader|rand}} |
||
< |
<syntaxhighlight lang="rust">// cargo-deps: rand |
||
extern crate rand; |
extern crate rand; |
||
Line 3,047: | Line 3,047: | ||
println!("{}", rng.gen_range(0, 20)); |
println!("{}", rng.gen_range(0, 20)); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|SAS}}== |
=={{header|SAS}}== |
||
< |
<syntaxhighlight lang="sas">data _null_; |
||
do while(1); |
do while(1); |
||
n=floor(uniform(0)*20); |
n=floor(uniform(0)*20); |
||
Line 3,056: | Line 3,056: | ||
if n=10 then leave; /* 'leave' to break a loop */ |
if n=10 then leave; /* 'leave' to break a loop */ |
||
end; |
end; |
||
run;</ |
run;</syntaxhighlight> |
||
=={{header|Sather}}== |
=={{header|Sather}}== |
||
< |
<syntaxhighlight lang="sather">-- help class for random number sequence |
||
class RANDOM is |
class RANDOM is |
||
attr seed:INT; |
attr seed:INT; |
||
Line 3,087: | Line 3,087: | ||
end; |
end; |
||
end; |
end; |
||
end;</ |
end;</syntaxhighlight> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">scala> import util.control.Breaks.{breakable, break} |
||
import util.control.Breaks.{breakable, break} |
import util.control.Breaks.{breakable, break} |
||
Line 3,109: | Line 3,109: | ||
4 |
4 |
||
10 |
10 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
(let loop ((first (random 20))) |
(let loop ((first (random 20))) |
||
(print first) |
(print first) |
||
Line 3,119: | Line 3,119: | ||
(print (random 20)) |
(print (random 20)) |
||
(loop (random 20))))) |
(loop (random 20))))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Or by using call/cc to break out: |
Or by using call/cc to break out: |
||
< |
<syntaxhighlight lang="scheme"> |
||
(call/cc |
(call/cc |
||
(lambda (break) |
(lambda (break) |
||
Line 3,132: | Line 3,132: | ||
(print (random 20)) |
(print (random 20)) |
||
(loop (random 20))))) |
(loop (random 20))))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Scilab}}== |
=={{header|Scilab}}== |
||
{{works with|Scilab|5.5.1}} |
{{works with|Scilab|5.5.1}} |
||
<lang>while %T |
<syntaxhighlight lang="text">while %T |
||
a=int(rand()*20) // [0..19] |
a=int(rand()*20) // [0..19] |
||
printf("%2d ",a) |
printf("%2d ",a) |
||
Line 3,143: | Line 3,143: | ||
printf("%2d\n",b) |
printf("%2d\n",b) |
||
end |
end |
||
printf("\n")</ |
printf("\n")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="height:20ex"> |
<pre style="height:20ex"> |
||
Line 3,163: | Line 3,163: | ||
is 10. The second random number does never terminate the loop. |
is 10. The second random number does never terminate the loop. |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
const proc: main is func |
const proc: main is func |
||
Line 3,176: | Line 3,176: | ||
end if; |
end if; |
||
until number = 10; |
until number = 10; |
||
end func;</ |
end func;</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">var lim = 20; |
||
loop { |
loop { |
||
say (var n = lim.rand.int); |
say (var n = lim.rand.int); |
||
n == 10 && break; |
n == 10 && break; |
||
say lim.rand.int; |
say lim.rand.int; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Simula}}== |
=={{header|Simula}}== |
||
{{works with|SIMULA-67}} |
{{works with|SIMULA-67}} |
||
< |
<syntaxhighlight lang="simula">! Loops/Break - simula67 - 08/03/2017; |
||
begin |
begin |
||
integer num,seed; |
integer num,seed; |
||
Line 3,199: | Line 3,199: | ||
end; |
end; |
||
lab: |
lab: |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,210: | Line 3,210: | ||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
{{works with|Smalltalk/X}} |
{{works with|Smalltalk/X}} |
||
< |
<syntaxhighlight lang="smalltalk">[ |
||
|first second done| |
|first second done| |
||
Line 3,220: | Line 3,220: | ||
]. |
]. |
||
done |
done |
||
] whileFalse</ |
] whileFalse</syntaxhighlight> |
||
alternative: |
alternative: |
||
< |
<syntaxhighlight lang="smalltalk">[:exit | |
||
|first| |
|first| |
||
Line 3,228: | Line 3,228: | ||
first == 10 ifTrue:[ exit value:nil ]. |
first == 10 ifTrue:[ exit value:nil ]. |
||
Stdout print:' '; printCR: (Random nextIntegerBetween:0 and:19). |
Stdout print:' '; printCR: (Random nextIntegerBetween:0 and:19). |
||
] loopWithExit.</ |
] loopWithExit.</syntaxhighlight> |
||
or shorter (because <tt>ifTrue:</tt> sends #value to its arg): |
or shorter (because <tt>ifTrue:</tt> sends #value to its arg): |
||
< |
<syntaxhighlight lang="smalltalk">[:exit | |
||
|first| |
|first| |
||
Line 3,236: | Line 3,236: | ||
first == 10 ifTrue:exit. |
first == 10 ifTrue:exit. |
||
Stdout print:' '; printCR: (Random nextIntegerBetween:0 and:19). |
Stdout print:' '; printCR: (Random nextIntegerBetween:0 and:19). |
||
] loopWithExit.</ |
] loopWithExit.</syntaxhighlight> |
||
=={{header|Snabel}}== |
=={{header|Snabel}}== |
||
Uses a ranged random generator as iterator. |
Uses a ranged random generator as iterator. |
||
< |
<syntaxhighlight lang="snabel"> |
||
let: rnd 19 random; |
let: rnd 19 random; |
||
Line 3,248: | Line 3,248: | ||
@rnd pop str say |
@rnd pop str say |
||
} for |
} for |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|SNOBOL4}}== |
=={{header|SNOBOL4}}== |
||
Most Snobols lack a built-in rand( ) function. Kludgy "Linux-only" implementation: |
Most Snobols lack a built-in rand( ) function. Kludgy "Linux-only" implementation: |
||
< |
<syntaxhighlight lang="snobol"> input(.random,io_findunit(),1,"/dev/urandom") |
||
while &ALPHABET random @rand |
while &ALPHABET random @rand |
||
output = rand = rand - (rand / 20) * 20 |
output = rand = rand - (rand / 20) * 20 |
||
eq(rand,10) :f(while) |
eq(rand,10) :f(while) |
||
end</ |
end</syntaxhighlight> |
||
Or using a library function: |
Or using a library function: |
||
< |
<syntaxhighlight lang="snobol4">* rand(n) -> real x | 0 <= x < n |
||
-include 'random.sno' |
-include 'random.sno' |
||
loop ne(output = convert(rand(20)'integer'),10) :s(loop) |
loop ne(output = convert(rand(20)'integer'),10) :s(loop) |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Spin}}== |
=={{header|Spin}}== |
||
Line 3,271: | Line 3,271: | ||
{{works with|HomeSpun}} |
{{works with|HomeSpun}} |
||
{{works with|OpenSpin}} |
{{works with|OpenSpin}} |
||
< |
<syntaxhighlight lang="spin">con |
||
_clkmode = xtal1 + pll16x |
_clkmode = xtal1 + pll16x |
||
_clkfreq = 80_000_000 |
_clkfreq = 80_000_000 |
||
Line 3,295: | Line 3,295: | ||
waitcnt(_clkfreq + cnt) |
waitcnt(_clkfreq + cnt) |
||
ser.stop |
ser.stop |
||
cogstop(0)</ |
cogstop(0)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,303: | Line 3,303: | ||
=={{header|SPL}}== |
=={{header|SPL}}== |
||
Direct approach: |
Direct approach: |
||
< |
<syntaxhighlight lang="spl">> |
||
n = #.rnd(20) |
n = #.rnd(20) |
||
#.output(n) |
#.output(n) |
||
Line 3,309: | Line 3,309: | ||
n = #.rnd(20) |
n = #.rnd(20) |
||
#.output(n) |
#.output(n) |
||
<</ |
<</syntaxhighlight> |
||
With reusable code: |
With reusable code: |
||
< |
<syntaxhighlight lang="spl">> |
||
:1 |
:1 |
||
n = #.rnd(20) |
n = #.rnd(20) |
||
Line 3,318: | Line 3,318: | ||
<< n=10 |
<< n=10 |
||
1 <-> |
1 <-> |
||
<</ |
<</syntaxhighlight> |
||
=={{header|SQL PL}}== |
=={{header|SQL PL}}== |
||
{{works with|Db2 LUW}} version 9.7 or higher. |
{{works with|Db2 LUW}} version 9.7 or higher. |
||
With SQL PL: |
With SQL PL: |
||
< |
<syntaxhighlight lang="sql pl"> |
||
--#SET TERMINATOR @ |
--#SET TERMINATOR @ |
||
Line 3,340: | Line 3,340: | ||
END WHILE LOOP; |
END WHILE LOOP; |
||
END @ |
END @ |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 3,358: | Line 3,358: | ||
</pre> |
</pre> |
||
Since V11.1, the builtin module can be used instead of RAND, like this: |
Since V11.1, the builtin module can be used instead of RAND, like this: |
||
< |
<syntaxhighlight lang="sql pl"> |
||
SET VAL = CALL DBMS_RANDOM.VALUE(0,20); |
SET VAL = CALL DBMS_RANDOM.VALUE(0,20); |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Stata}}== |
=={{header|Stata}}== |
||
< |
<syntaxhighlight lang="stata">while 1 { |
||
local n=runiformint(0,19) |
local n=runiformint(0,19) |
||
display `n' |
display `n' |
||
if `n'==10 continue, break |
if `n'==10 continue, break |
||
display runiformint(0,19) |
display runiformint(0,19) |
||
}</ |
}</syntaxhighlight> |
||
=== Mata === |
=== Mata === |
||
< |
<syntaxhighlight lang="stata">for (; 1; ) { |
||
printf("%f\n",n=runiformint(1,1,0,19)) |
printf("%f\n",n=runiformint(1,1,0,19)) |
||
if (n==10) break |
if (n==10) break |
||
printf("%f\n",runiformint(1,1,0,19)) |
printf("%f\n",runiformint(1,1,0,19)) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Suneido}}== |
=={{header|Suneido}}== |
||
< |
<syntaxhighlight lang="suneido">forever |
||
{ |
{ |
||
Print(i = Random(20)) |
Print(i = Random(20)) |
||
Line 3,385: | Line 3,385: | ||
Print(i = Random(20)) |
Print(i = Random(20)) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">while true |
||
{ |
{ |
||
let a = Int(arc4random()) % (20) |
let a = Int(arc4random()) % (20) |
||
Line 3,399: | Line 3,399: | ||
print("b: \(b)") |
print("b: \(b)") |
||
} |
} |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
a: 2 b: 7 |
a: 2 b: 7 |
||
Line 3,408: | Line 3,408: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">while true { |
||
set a [expr int(20*rand())] |
set a [expr int(20*rand())] |
||
puts $a |
puts $a |
||
Line 3,416: | Line 3,416: | ||
set b [expr int(20*rand())] |
set b [expr int(20*rand())] |
||
puts $b |
puts $b |
||
}</ |
}</syntaxhighlight> |
||
=={{header|TI-89 BASIC}}== |
=={{header|TI-89 BASIC}}== |
||
< |
<syntaxhighlight lang="ti89b">Local x |
||
Loop |
Loop |
||
rand(20)-1 → x |
rand(20)-1 → x |
||
Line 3,428: | Line 3,428: | ||
EndIf |
EndIf |
||
Output 64, 50, rand(20)-1 © paint text to the right on same line |
Output 64, 50, rand(20)-1 © paint text to the right on same line |
||
EndLoop</ |
EndLoop</syntaxhighlight> |
||
=={{header|TorqueScript}}== |
=={{header|TorqueScript}}== |
||
< |
<syntaxhighlight lang="torque">for(%a = 0; %a > -1; %a++) |
||
{ |
{ |
||
%number = getRandom(0, 19); |
%number = getRandom(0, 19); |
||
if(%number == 10) |
if(%number == 10) |
||
break; |
break; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Transact-SQL}}== |
=={{header|Transact-SQL}}== |
||
<syntaxhighlight lang="transact-sql"> |
|||
<lang Transact-SQL> |
|||
DECLARE @i INT; |
DECLARE @i INT; |
||
WHILE 1=1 |
WHILE 1=1 |
||
Line 3,450: | Line 3,450: | ||
PRINT ABS(CHECKSUM(NewId())) % 20; |
PRINT ABS(CHECKSUM(NewId())) % 20; |
||
END; |
END; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|TUSCRIPT}}== |
=={{header|TUSCRIPT}}== |
||
< |
<syntaxhighlight lang="tuscript"> |
||
$$ MODE TUSCRIPT |
$$ MODE TUSCRIPT |
||
LOOP |
LOOP |
||
Line 3,466: | Line 3,466: | ||
IF (10==a,b) STOP |
IF (10==a,b) STOP |
||
ENDLOOP |
ENDLOOP |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,480: | Line 3,480: | ||
=={{header|uBasic/4tH}}== |
=={{header|uBasic/4tH}}== |
||
<lang>Do |
<syntaxhighlight lang="text">Do |
||
n = RND(20) |
n = RND(20) |
||
Print n |
Print n |
||
Until n = 10 |
Until n = 10 |
||
Print RND(20) |
Print RND(20) |
||
Loop</ |
Loop</syntaxhighlight> |
||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
Line 3,493: | Line 3,493: | ||
{{works with|Bourne Shell}} |
{{works with|Bourne Shell}} |
||
{{libheader|jot}} |
{{libheader|jot}} |
||
< |
<syntaxhighlight lang="bash">while true; do |
||
a=`jot -w %d -r 1 0 20` || exit $? |
a=`jot -w %d -r 1 0 20` || exit $? |
||
echo $a |
echo $a |
||
Line 3,499: | Line 3,499: | ||
b=`jot -w %d -r 1 0 20` || exit $? |
b=`jot -w %d -r 1 0 20` || exit $? |
||
echo $b |
echo $b |
||
done</ |
done</syntaxhighlight> |
||
Korn Shells have a RANDOM parameter. |
Korn Shells have a RANDOM parameter. |
||
Line 3,505: | Line 3,505: | ||
{{works with|Bash}} |
{{works with|Bash}} |
||
{{works with|pdksh|5.2.14}} |
{{works with|pdksh|5.2.14}} |
||
< |
<syntaxhighlight lang="bash">while true; do |
||
echo $((a=RANDOM%20)) |
echo $((a=RANDOM%20)) |
||
[ $a -eq 10 ] && break |
[ $a -eq 10 ] && break |
||
echo $((b=RANDOM%20)) |
echo $((b=RANDOM%20)) |
||
done</ |
done</syntaxhighlight> |
||
=={{header|Ursa}}== |
=={{header|Ursa}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="ursa">decl ursa.util.random r |
||
decl int a b |
decl int a b |
||
while true |
while true |
||
Line 3,523: | Line 3,523: | ||
set b (r.getint 19) |
set b (r.getint 19) |
||
out b endl console |
out b endl console |
||
end while</ |
end while</syntaxhighlight> |
||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
< |
<syntaxhighlight lang="vb">Public Sub LoopsBreak() |
||
Dim value As Integer |
Dim value As Integer |
||
Randomize |
Randomize |
||
Line 3,535: | Line 3,535: | ||
Debug.Print Int(20 * Rnd) |
Debug.Print Int(20 * Rnd) |
||
Loop |
Loop |
||
End Sub</ |
End Sub</syntaxhighlight> |
||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
Based on BASIC version. Demonstrates breaking out of Do/Loop and For/Next (Exit is good for getting out of functions and subs as well). |
Based on BASIC version. Demonstrates breaking out of Do/Loop and For/Next (Exit is good for getting out of functions and subs as well). |
||
< |
<syntaxhighlight lang="vb">Dim a, b, i |
||
Do |
Do |
||
Line 3,556: | Line 3,556: | ||
b = Int(Rnd * 20) |
b = Int(Rnd * 20) |
||
WScript.Echo vbNullString, b |
WScript.Echo vbNullString, b |
||
Next</ |
Next</syntaxhighlight> |
||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
< |
<syntaxhighlight lang="vbnet">Module Program |
||
Sub Main() |
Sub Main() |
||
' Initialize with seed 0 to get deterministic output (may vary across .NET versions, though). |
' Initialize with seed 0 to get deterministic output (may vary across .NET versions, though). |
||
Line 3,575: | Line 3,575: | ||
Loop |
Loop |
||
End Sub |
End Sub |
||
End Module</ |
End Module</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>14 16 15 11 4 11 18 8 19 5 5 9 12 9 19 0 17 19 13 6 16 16 19 0 13 10 18 13 10 </pre> |
<pre>14 16 15 11 4 11 18 8 19 5 5 9 12 9 19 0 17 19 13 6 16 16 19 0 13 10 18 13 10 </pre> |
||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
< |
<syntaxhighlight lang="vlang">import rand |
||
import rand.seed |
import rand.seed |
||
Line 3,594: | Line 3,594: | ||
println(b) |
println(b) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
< |
<syntaxhighlight lang="ecmascript">import "random" for Random |
||
var r = Random.new() |
var r = Random.new() |
||
Line 3,605: | Line 3,605: | ||
if (n == 10) break |
if (n == 10) break |
||
System.print(r.int(20)) |
System.print(r.int(20)) |
||
} </ |
} </syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,621: | Line 3,621: | ||
=={{header|XBasic}}== |
=={{header|XBasic}}== |
||
{{works with|Windows XBasic}} |
{{works with|Windows XBasic}} |
||
< |
<syntaxhighlight lang="xbasic"> |
||
PROGRAM "loopbreak" |
PROGRAM "loopbreak" |
||
Line 3,658: | Line 3,658: | ||
END PROGRAM |
END PROGRAM |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,675: | Line 3,675: | ||
=={{header|XBS}}== |
=={{header|XBS}}== |
||
< |
<syntaxhighlight lang="xbs">while(true){ |
||
set n:number = math.random(0,19); |
set n:number = math.random(0,19); |
||
log(`first: {n}`); |
log(`first: {n}`); |
||
Line 3,681: | Line 3,681: | ||
n = math.random(0,19); |
n = math.random(0,19); |
||
log(`second: {n}`); |
log(`second: {n}`); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,698: | Line 3,698: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; |
||
int N; |
int N; |
||
loop [N:= Ran(20); |
loop [N:= Ran(20); |
||
Line 3,706: | Line 3,706: | ||
IntOut(0, Ran(20)); |
IntOut(0, Ran(20)); |
||
CrLf(0); |
CrLf(0); |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,722: | Line 3,722: | ||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
< |
<syntaxhighlight lang="yabasic">do |
||
i = int(ran(19)) |
i = int(ran(19)) |
||
print i using "##"; |
print i using "##"; |
||
Line 3,731: | Line 3,731: | ||
loop |
loop |
||
print |
print |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Zig}}== |
=={{header|Zig}}== |
||
< |
<syntaxhighlight lang="zig">const std = @import("std"); |
||
pub fn main() !void { |
pub fn main() !void { |
||
Line 3,750: | Line 3,750: | ||
try std.io.getStdOut().writer().print("{d}\n", .{rand_num2}); |
try std.io.getStdOut().writer().print("{d}\n", .{rand_num2}); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">while(1){n:=(0).random(20); n.print(" "); |
||
if (n==10){ println(); break; } (0).random().println(); |
if (n==10){ println(); break; } (0).random().println(); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |