RPG attributes generator: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 36: Line 36:
{{trans|Python}}
{{trans|Python}}


<lang 11l>random:seed(Int(Time().unix_time()))
<syntaxhighlight lang="11l">random:seed(Int(Time().unix_time()))
V total = 0
V total = 0
V count = 0
V count = 0
Line 50: Line 50:
total = sum(attributes)
total = sum(attributes)


print(total‘ ’attributes)</lang>
print(total‘ ’attributes)</syntaxhighlight>


{{out}}
{{out}}
Line 59: Line 59:
=={{header|8086 Assembly}}==
=={{header|8086 Assembly}}==


<lang asm> bits 16
<syntaxhighlight lang="asm"> bits 16
cpu 8086
cpu 8086
putch: equ 2h
putch: equ 2h
Line 147: Line 147:
section .bss
section .bss
rnddat: resb 4 ; RNG state
rnddat: resb 4 ; RNG state
attrs: resb 6 ; Rolled attributes</lang>
attrs: resb 6 ; Rolled attributes</syntaxhighlight>


{{out}}
{{out}}
Line 162: Line 162:
=={{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 rpg64.s */
/* program rpg64.s */
Line 398: Line 398:
/* 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 413: Line 413:


=={{header|Action!}}==
=={{header|Action!}}==
<lang Action!>TYPE Result=[BYTE success,sum,highCount]
<syntaxhighlight lang="action!">TYPE Result=[BYTE success,sum,highCount]
BYTE FUNC GenerateAttrib()
BYTE FUNC GenerateAttrib()
BYTE i,v,min,sum
BYTE i,v,min,sum
Line 473: Line 473:
FI
FI
OD
OD
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/RPG_attributes_generator.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/RPG_attributes_generator.png Screenshot from Atari 8-bit computer]
Line 485: Line 485:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{Trans|Action!}}
{{Trans|Action!}}
<lang algol68>BEGIN # RPG attributes generator #
<syntaxhighlight lang="algol68">BEGIN # RPG attributes generator #


INT attrib count = 6;
INT attrib count = 6;
Line 537: Line 537:
OD
OD


END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 549: Line 549:
{{works with|Dyalog APL}}
{{works with|Dyalog APL}}


<lang APL>roll←{(+/-⌊/)¨?¨6/⊂4/6}⍣{(75≤+/⍺)∧2≤+/⍺≥15}</lang>
<syntaxhighlight lang="apl">roll←{(+/-⌊/)¨?¨6/⊂4/6}⍣{(75≤+/⍺)∧2≤+/⍺≥15}</syntaxhighlight>


{{out}}
{{out}}
Line 564: Line 564:
=={{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 888: Line 888:
bx lr
bx lr


</syntaxhighlight>
</lang>


=={{header|Arturo}}==
=={{header|Arturo}}==


<lang rebol>vals: []
<syntaxhighlight lang="rebol">vals: []


while [or? 75 > sum vals
while [or? 75 > sum vals
Line 906: Line 906:


print ["values:" vals ]
print ["values:" vals ]
print ["with sum:" sum vals]</lang>
print ["with sum:" sum vals]</syntaxhighlight>


=={{header|Atari BASIC}}==
=={{header|Atari BASIC}}==
{{trans|Commodore BASIC}}
{{trans|Commodore BASIC}}
<lang basic>100 REM RPG character generator
<syntaxhighlight lang="basic">100 REM RPG character generator
110 DIM AT(5)
110 DIM AT(5)
120 DIM AT$(18)
120 DIM AT$(18)
Line 948: Line 948:
450 IF K=78 THEN 140
450 IF K=78 THEN 140
460 POSITION 0,13
460 POSITION 0,13
470 PRINT "Excellent. Good luck on your adventure!"</lang>
470 PRINT "Excellent. Good luck on your adventure!"</syntaxhighlight>


{{Out}}
{{Out}}
Line 971: Line 971:
=={{header|BASIC256}}==
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang freebasic>function min(a, b)
<syntaxhighlight lang="freebasic">function min(a, b)
if a < b then return a else return b
if a < b then return a else return b
end function
end function
Line 1,006: Line 1,006:
print "-------"
print "-------"
print "TOT: "; sum
print "TOT: "; sum
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>Igual que la entrada de FreeBASIC.</pre>
<pre>Igual que la entrada de FreeBASIC.</pre>
Line 1,012: Line 1,012:
=={{header|C}}==
=={{header|C}}==
{{trans|Go}}
{{trans|Go}}
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <time.h>
#include <time.h>
Line 1,053: Line 1,053:
}
}
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,066: Line 1,066:
=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
{{trans|Visual Basic .NET}}
{{trans|Visual Basic .NET}}
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Linq;
Line 1,097: Line 1,097:
} while (!good);
} while (!good);
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
sample outputs:
sample outputs:
Line 1,106: Line 1,106:
=={{header|C++}}==
=={{header|C++}}==
GCC 4.9.2, unoptimised.
GCC 4.9.2, unoptimised.
<lang cpp>#include <algorithm>
<syntaxhighlight lang="cpp">#include <algorithm>
#include <ctime>
#include <ctime>
#include <iostream>
#include <iostream>
Line 1,157: Line 1,157:
return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
Sample run:
Sample run:
Line 1,166: Line 1,166:


=={{header|Caché ObjectScript}}==
=={{header|Caché ObjectScript}}==
<syntaxhighlight lang="caché objectscript">RPGGEN
<lang Caché ObjectScript>RPGGEN
set attr = $lb("") ; empty list to start
set attr = $lb("") ; empty list to start
write "Rules:",!,"1.) Total of 6 attributes must be at least 75.",!,"2.) At least two scores must be 15 or more.",!
write "Rules:",!,"1.) Total of 6 attributes must be at least 75.",!,"2.) At least two scores must be 15 or more.",!
Line 1,211: Line 1,211:
} while (result '= 3)
} while (result '= 3)
quit</lang>
quit</syntaxhighlight>
{{out}}<pre>
{{out}}<pre>
Line 1,227: Line 1,227:


=={{header|CLU}}==
=={{header|CLU}}==
<lang clu>% This program needs to be merged with PCLU's "misc" library
<syntaxhighlight lang="clu">% This program needs to be merged with PCLU's "misc" library
% to use the random number generator.
% to use the random number generator.
%
%
Line 1,296: Line 1,296:
stream$putright(po, int$unparse(stat), 4)
stream$putright(po, int$unparse(stat), 4)
end
end
end start_up</lang>
end start_up</syntaxhighlight>
{{out}}
{{out}}
<pre>$ ./rpg_gen
<pre>$ ./rpg_gen
Line 1,312: Line 1,312:
Except for screen control codes, this is generic enough it could be used for many other 8-bit interpreted BASICs as well. (Applesoft, ZX Spectrum, etc.). Should work on all Commodore models. (Adjustment for screen width may be necessary on VIC-20.)
Except for screen control codes, this is generic enough it could be used for many other 8-bit interpreted BASICs as well. (Applesoft, ZX Spectrum, etc.). Should work on all Commodore models. (Adjustment for screen width may be necessary on VIC-20.)


<lang gwbasic>100 rem rpg character roller
<syntaxhighlight lang="gwbasic">100 rem rpg character roller
110 rem rosetta code - commodore basic
110 rem rosetta code - commodore basic
120 dim di(3):rem dice
120 dim di(3):rem dice
Line 1,351: Line 1,351:
470 if di(x)<di(x+1) then t=di(x):di(x)=di(x+1):di(x+1)=t
470 if di(x)<di(x+1) then t=di(x):di(x)=di(x+1):di(x+1)=t
480 next x
480 next x
490 return</lang>
490 return</syntaxhighlight>


{{out}}
{{out}}
Line 1,395: Line 1,395:


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<lang lisp>
<syntaxhighlight lang="lisp">
(defpackage :rpg-generator
(defpackage :rpg-generator
(:use :cl)
(:use :cl)
Line 1,414: Line 1,414:
(format t ">= 15: ~A~%" (count-if (lambda (n) (>= n 15)) scores))
(format t ">= 15: ~A~%" (count-if (lambda (n) (>= n 15)) scores))
(return scores)))
(return scores)))
</syntaxhighlight>
</lang>
=={{header|Cowgol}}==
=={{header|Cowgol}}==
<lang cowgol>include "cowgol.coh";
<syntaxhighlight lang="cowgol">include "cowgol.coh";
include "argv.coh";
include "argv.coh";


Line 1,513: Line 1,513:
print("\nTotal: ");
print("\nTotal: ");
print_i8(total);
print_i8(total);
print_nl();</lang>
print_nl();</syntaxhighlight>


{{out}}
{{out}}
Line 1,528: Line 1,528:


=={{header|Crystal}}==
=={{header|Crystal}}==
<lang Ruby>def roll_stat
<syntaxhighlight lang="ruby">def roll_stat
dices = Array(Int32).new(4) { rand(1..6) }
dices = Array(Int32).new(4) { rand(1..6) }
dices.sum - dices.min
dices.sum - dices.min
Line 1,543: Line 1,543:
stats = roll_character
stats = roll_character
puts "stats: #{stats}, sum is #{stats.sum}"
puts "stats: #{stats}, sum is #{stats.sum}"
end</lang>
end</syntaxhighlight>


sample output:
sample output:
Line 1,561: Line 1,561:
{{libheader| System.Generics.Collections}}
{{libheader| System.Generics.Collections}}
{{Trans|C#}}
{{Trans|C#}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program RPG_Attributes_Generator;
program RPG_Attributes_Generator;


Line 1,675: Line 1,675:
end.
end.


</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,684: Line 1,684:
=={{header|Dyalect}}==
=={{header|Dyalect}}==
{{trans|C#}}
{{trans|C#}}
<lang dyalect>func getThree(n) {
<syntaxhighlight lang="dyalect">func getThree(n) {
var g3 = []
var g3 = []
for i in 0..33 {
for i in 0..33 {
Line 1,727: Line 1,727:
good = gs.Sum() >= 75 && hvc > 1
good = gs.Sum() >= 75 && hvc > 1
print(" - " + (good ? "success" : "failure"))
print(" - " + (good ? "success" : "failure"))
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,735: Line 1,735:
=={{header|EasyLang}}==
=={{header|EasyLang}}==


<lang>len v[] 6
<syntaxhighlight lang="text">len v[] 6
repeat
repeat
vsum = 0
vsum = 0
Line 1,759: Line 1,759:
.
.
print "Attributes: " & " " & v[]
print "Attributes: " & " " & v[]
print "Total: " & " " & vsum</lang>
print "Total: " & " " & vsum</syntaxhighlight>


<pre>
<pre>
Line 1,768: Line 1,768:
=={{header|Factor}}==
=={{header|Factor}}==
{{Works with|Factor|0.98}}
{{Works with|Factor|0.98}}
<lang factor>USING: combinators.short-circuit dice formatting io kernel math
<syntaxhighlight lang="factor">USING: combinators.short-circuit dice formatting io kernel math
math.statistics qw sequences ;
math.statistics qw sequences ;
IN: rosetta-code.rpg-attributes-generator
IN: rosetta-code.rpg-attributes-generator
Line 1,793: Line 1,793:
[ "%s: %d\n" printf ] 2each nl stats-info ;
[ "%s: %d\n" printf ] 2each nl stats-info ;
MAIN: main</lang>
MAIN: main</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,808: Line 1,808:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>#define min(a, b) iif(a < b, a, b)
<syntaxhighlight lang="freebasic">#define min(a, b) iif(a < b, a, b)


function d6() as integer
function d6() as integer
Line 1,841: Line 1,841:
next i
next i
print "--------"
print "--------"
print using "TOT: ##";sum</lang>
print using "TOT: ##";sum</syntaxhighlight>
{{out}}<pre>STR: 14
{{out}}<pre>STR: 14
CON: 11
CON: 11
Line 1,853: Line 1,853:


=={{header|FOCAL}}==
=={{header|FOCAL}}==
<lang FOCAL>01.10 S T=0
<syntaxhighlight lang="focal">01.10 S T=0
01.20 F X=1,6;D 4;S AT(X)=S3;S T=T+S3
01.20 F X=1,6;D 4;S AT(X)=S3;S T=T+S3
01.30 I (T-75)1.1
01.30 I (T-75)1.1
Line 1,876: Line 1,876:
06.01 C--ROLL A D6
06.01 C--ROLL A D6
06.10 S A=FRAN()*10;S A=A-FITR(A)
06.10 S A=FRAN()*10;S A=A-FITR(A)
06.20 S A=1+FITR(A*6)</lang>
06.20 S A=1+FITR(A*6)</syntaxhighlight>


{{out}}
{{out}}
Line 1,902: Line 1,902:
{{works with|GNU Forth}}
{{works with|GNU Forth}}
{{libheader|random.fs}}
{{libheader|random.fs}}
<lang forth>require random.fs
<syntaxhighlight lang="forth">require random.fs
: d6 ( -- roll ) 6 random 1 + ;
: d6 ( -- roll ) 6 random 1 + ;


Line 1,937: Line 1,937:
;
;


utime drop seed !</lang>
utime drop seed !</syntaxhighlight>


{{Out}}
{{Out}}
Line 1,944: Line 1,944:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 1,989: Line 1,989:
break
break
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,001: Line 2,001:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Control.Monad (replicateM)
<syntaxhighlight lang="haskell">import Control.Monad (replicateM)
import System.Random (randomRIO)
import System.Random (randomRIO)
import Data.Bool (bool)
import Data.Bool (bool)
Line 2,019: Line 2,019:
-------------------------- TEST ---------------------------
-------------------------- TEST ---------------------------
main :: IO ()
main :: IO ()
main = replicateM 10 character >>= mapM_ (print . (sum >>= (,)))</lang>
main = replicateM 10 character >>= mapM_ (print . (sum >>= (,)))</syntaxhighlight>
{{Out}}
{{Out}}
<pre>Sample computation:
<pre>Sample computation:
Line 2,036: Line 2,036:
=={{header|J}}==
=={{header|J}}==
'twould be more efficient to work with index origin 0, then increment the roll once at output.
'twould be more efficient to work with index origin 0, then increment the roll once at output.
<syntaxhighlight lang="j">
<lang J>
roll=: 1 + 4 6 ?@:$ 6:
roll=: 1 + 4 6 ?@:$ 6:
massage=: +/ - <./
massage=: +/ - <./
Line 2,049: Line 2,049:
NB. use: generate_character 'name'
NB. use: generate_character 'name'
generate_character=: (; (+/ ; ])@:([: generate_attributes@:show Until accept 0:))&>@:boxopen
generate_character=: (; (+/ ; ])@:([: generate_attributes@:show Until accept 0:))&>@:boxopen
</syntaxhighlight>
</lang>




Line 2,082: Line 2,082:


=={{header|Java}}==
=={{header|Java}}==
<lang Java>import java.util.List;
<syntaxhighlight lang="java">import java.util.List;
import java.util.Random;
import java.util.Random;
import java.util.stream.Stream;
import java.util.stream.Stream;
Line 2,115: Line 2,115:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{Out}}
{{Out}}
<pre>
<pre>
Line 2,124: Line 2,124:
=={{header|Javascript}}==
=={{header|Javascript}}==
===Imperative===
===Imperative===
<lang javascript>function roll() {
<syntaxhighlight lang="javascript">function roll() {
const stats = {
const stats = {
total: 0,
total: 0,
Line 2,157: Line 2,157:
${rolledCharacter.rolls.join(', ')}
${rolledCharacter.rolls.join(', ')}


Their sum is ${rolledCharacter.total} and ${rolledCharacter.rolls.filter(a => a >= 15).length} of them are >= 15`);</lang>
Their sum is ${rolledCharacter.total} and ${rolledCharacter.rolls.filter(a => a >= 15).length} of them are >= 15`);</syntaxhighlight>


{{out}}
{{out}}
Line 2,170: Line 2,170:
===Functional===
===Functional===
{{Trans|Python}} (Functional composition version)
{{Trans|Python}} (Functional composition version)
<lang javascript>(() => {
<syntaxhighlight lang="javascript">(() => {
'use strict';
'use strict';


Line 2,293: Line 2,293:
// MAIN ---
// MAIN ---
return main();
return main();
})();</lang>
})();</syntaxhighlight>
{{Out}}
{{Out}}
A sample of 10 character attribute sets:
A sample of 10 character attribute sets:
Line 2,308: Line 2,308:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>roll_skip_lowest(dice, sides) = (r = rand(collect(1:sides), dice); sum(r) - minimum(r))
<syntaxhighlight lang="julia">roll_skip_lowest(dice, sides) = (r = rand(collect(1:sides), dice); sum(r) - minimum(r))


function rollRPGtoon()
function rollRPGtoon()
Line 2,329: Line 2,329:
rollRPGtoon()
rollRPGtoon()
rollRPGtoon()
rollRPGtoon()
</lang>{{output}}<pre>
</syntaxhighlight>{{output}}<pre>
New RPG character roll: [15, 16, 15, 11, 9, 15]. Sum is 81, and 4 are >= 15.
New RPG character roll: [15, 16, 15, 11, 9, 15]. Sum is 81, and 4 are >= 15.
New RPG character roll: [12, 14, 15, 12, 10, 16]. Sum is 79, and 2 are >= 15.
New RPG character roll: [12, 14, 15, 12, 10, 16]. Sum is 79, and 2 are >= 15.
Line 2,336: Line 2,336:


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang kotlin>import kotlin.random.Random
<syntaxhighlight lang="kotlin">import kotlin.random.Random


fun main() {
fun main() {
Line 2,351: Line 2,351:
break
break
}
}
}</lang>
}</syntaxhighlight>


{{output}}
{{output}}
Line 2,361: Line 2,361:


=={{header|Ksh}}==
=={{header|Ksh}}==
<lang ksh>
<syntaxhighlight lang="ksh">
#!/bin/ksh
#!/bin/ksh


Line 2,432: Line 2,432:
print "Attribute value total: ${total}"
print "Attribute value total: ${total}"
print "Attribule count >= 15: ${cnt15}"
print "Attribule count >= 15: ${cnt15}"
</syntaxhighlight>
</lang>
{{out}}<pre>
{{out}}<pre>
strength: 11
strength: 11
Line 2,444: Line 2,444:


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>valid = False;
<syntaxhighlight lang="mathematica">valid = False;
While[! valid,
While[! valid,
try = Map[Total[TakeLargest[#, 3]] &,
try = Map[Total[TakeLargest[#, 3]] &,
Line 2,452: Line 2,452:
]
]
]
]
{Total[try], try}</lang>
{Total[try], try}</syntaxhighlight>
{{out}}
{{out}}
<pre>{78, {13, 15, 9, 13, 12, 16}}</pre>
<pre>{78, {13, 15, 9, 13, 12, 16}}</pre>
Line 2,458: Line 2,458:
=={{header|min}}==
=={{header|min}}==
{{works with|min|0.19.6}}
{{works with|min|0.19.6}}
<lang min>randomize ; Seed the rng with current timestamp.
<syntaxhighlight lang="min">randomize ; Seed the rng with current timestamp.


; Implement some general operators we'll need that aren't in the library.
; Implement some general operators we'll need that aren't in the library.
Line 2,474: Line 2,474:
(big (dup special?) () (pop big) () linrec) :stats ; Roll a set of big and special attributes.
(big (dup special?) () (pop big) () linrec) :stats ; Roll a set of big and special attributes.


stats puts "Total: " print! sum puts!</lang>
stats puts "Total: " print! sum puts!</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,482: Line 2,482:


=={{header|MiniScript}}==
=={{header|MiniScript}}==
<lang MiniScript>roll = function()
<syntaxhighlight lang="miniscript">roll = function()
results = []
results = []
for i in range(0,3)
for i in range(0,3)
Line 2,508: Line 2,508:
end while
end while
print "Success!"
print "Success!"
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>Attribute values: 11, 13, 8, 10, 8, 10
<pre>Attribute values: 11, 13, 8, 10, 8, 10
Line 2,527: Line 2,527:


=={{header|Nim}}==
=={{header|Nim}}==
<syntaxhighlight lang="nim">
<lang Nim>
# Import "random" to get random numbers and "algorithm" to get sorting functions for arrays.
# Import "random" to get random numbers and "algorithm" to get sorting functions for arrays.
import random, algorithm
import random, algorithm
Line 2,561: Line 2,561:
if not twoFifteens(sixAttr) or sumAttr < 75: echo "Not good enough. Rerolling..."
if not twoFifteens(sixAttr) or sumAttr < 75: echo "Not good enough. Rerolling..."
else: break
else: break
</syntaxhighlight>
</lang>


Sample output: <pre>
Sample output: <pre>
Line 2,572: Line 2,572:
=={{header|OCaml}}==
=={{header|OCaml}}==
Original version by [http://rosettacode.org/wiki/User:Vanyamil User:Vanyamil]
Original version by [http://rosettacode.org/wiki/User:Vanyamil User:Vanyamil]
<syntaxhighlight lang="ocaml">
<lang OCaml>
(* Task : RPG_attributes_generator *)
(* Task : RPG_attributes_generator *)


Line 2,608: Line 2,608:
let s = valid_set () in
let s = valid_set () in
List.iter (fun i -> print_int i; print_string ", ") s
List.iter (fun i -> print_int i; print_string ", ") s
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,616: Line 2,616:
=={{header|Pascal|FreePascal}}==
=={{header|Pascal|FreePascal}}==


<syntaxhighlight lang="pascal">
<lang Pascal>
program attributes;
program attributes;


Line 2,656: Line 2,656:
writeln(' ---');
writeln(' ---');
end.
end.
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,691: Line 2,691:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use List::Util 'sum';
use List::Util 'sum';


Line 2,710: Line 2,710:


printf "%s = %2d\n", $attr_names[$_], $attr[$_] for 0..$#attr;
printf "%s = %2d\n", $attr_names[$_], $attr[$_] for 0..$#attr;
printf "Sum = %d, with %d attributes >= $hero_attr_min\n", sum(@attr), heroic(@attr);</lang>
printf "Sum = %d, with %d attributes >= $hero_attr_min\n", sum(@attr), heroic(@attr);</syntaxhighlight>
{{out}}
{{out}}
<pre>Str = 13
<pre>Str = 13
Line 2,721: Line 2,721:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">numbers</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">numbers</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">)</span>
Line 2,740: Line 2,740:
<span style="color: #000000;">numbers</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">numbers</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;">"\nTheir sum is %d and %d of them are &gt;=15\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</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;">"\nTheir sum is %d and %d of them are &gt;=15\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">})</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 2,753: Line 2,753:
=={{header|PHP}}==
=={{header|PHP}}==
===Version 1===
===Version 1===
<lang php><?php
<syntaxhighlight lang="php"><?php


$attributesTotal = 0;
$attributesTotal = 0;
Line 2,783: Line 2,783:
}
}


print_r($attributes);</lang>
print_r($attributes);</syntaxhighlight>


===Version 2===
===Version 2===
<lang php><?php
<syntaxhighlight lang="php"><?php
class CharacterGenerator {
class CharacterGenerator {
Line 2,825: Line 2,825:
print "Attribute Total: $attributesTotal\n";
print "Attribute Total: $attributesTotal\n";
print "Attributes: $attributesString";</lang>
print "Attributes: $attributesString";</syntaxhighlight>


{{out}}
{{out}}
Line 2,832: Line 2,832:


=={{header|Plain English}}==
=={{header|Plain English}}==
<lang plainenglish>To add an attribute to some stats:
<syntaxhighlight lang="plainenglish">To add an attribute to some stats:
Allocate memory for an entry.
Allocate memory for an entry.
Put the attribute into the entry's attribute.
Put the attribute into the entry's attribute.
Line 2,911: Line 2,911:
If the entry's next is not nil, write ", " on the console without advancing.
If the entry's next is not nil, write ", " on the console without advancing.
Put the entry's next into the entry.
Put the entry's next into the entry.
Repeat.</lang>
Repeat.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,920: Line 2,920:


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang purebasic>#heroicAttributeMinimum = 15
<syntaxhighlight lang="purebasic">#heroicAttributeMinimum = 15
#heroicAttributeCountMinimum = 2
#heroicAttributeCountMinimum = 2
#attributeSumMinimum = 75
#attributeSumMinimum = 75
Line 2,976: Line 2,976:
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
CloseConsole()
EndIf</lang>
EndIf</syntaxhighlight>
Sample output:
Sample output:
<pre>Attributes generated: [13, 17, 17, 11, 9, 17]
<pre>Attributes generated: [13, 17, 17, 11, 9, 17]
Line 2,983: Line 2,983:
=={{header|Python}}==
=={{header|Python}}==
===Python: Simple===
===Python: Simple===
<lang python>import random
<syntaxhighlight lang="python">import random
random.seed()
random.seed()
attributes_total = 0
attributes_total = 0
Line 3,009: Line 3,009:
attributes_total = sum(attributes)
attributes_total = sum(attributes)
print(attributes_total, attributes)</lang>
print(attributes_total, attributes)</syntaxhighlight>


{{out}}
{{out}}
Line 3,016: Line 3,016:


===Python: Nested Comprehensions #1===
===Python: Nested Comprehensions #1===
<lang python>import random
<syntaxhighlight lang="python">import random
random.seed()
random.seed()
total = 0
total = 0
Line 3,030: Line 3,030:
total = sum(attributes)
total = sum(attributes)
print(total, attributes)</lang>
print(total, attributes)</syntaxhighlight>


{{out}}
{{out}}
Line 3,038: Line 3,038:
===Python: Nested Comprehensions #2===
===Python: Nested Comprehensions #2===
With comprehensions for checking candidate values in the while expression.
With comprehensions for checking candidate values in the while expression.
<lang python>import random
<syntaxhighlight lang="python">import random


def compute():
def compute():
Line 3,049: Line 3,049:
for i in range(3):
for i in range(3):
print(*compute())
print(*compute())
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 3,059: Line 3,059:
Composing a hero-generator from reusable functions:
Composing a hero-generator from reusable functions:
{{Works with|Python|3.7}}
{{Works with|Python|3.7}}
<lang python>'''RPG Attributes Generator'''
<syntaxhighlight lang="python">'''RPG Attributes Generator'''


from itertools import islice
from itertools import islice
Line 3,175: Line 3,175:


if __name__ == '__main__':
if __name__ == '__main__':
main()</lang>
main()</syntaxhighlight>
<pre>A sample of 10:
<pre>A sample of 10:


Line 3,191: Line 3,191:
===Python: One-liner===
===Python: One-liner===
Just because you can, doesn't mean you should.
Just because you can, doesn't mean you should.
<lang python>
<syntaxhighlight lang="python">
import random; print((lambda attr: f"Attributes: {attr}\nTotal: {sum(attr)}")((lambda func, roll_func: func(func, roll_func, roll_func()))((lambda func, roll_func, rolls: rolls if sum(rolls) >= 75 and rolls.count(15) >= 2 else func(func, roll_func, roll_func())), lambda: [sum(sorted(random.randint(1, 6) for _ in range(4))[1:]) for _ in range(6)])))
import random; print((lambda attr: f"Attributes: {attr}\nTotal: {sum(attr)}")((lambda func, roll_func: func(func, roll_func, roll_func()))((lambda func, roll_func, rolls: rolls if sum(rolls) >= 75 and rolls.count(15) >= 2 else func(func, roll_func, roll_func())), lambda: [sum(sorted(random.randint(1, 6) for _ in range(4))[1:]) for _ in range(6)])))
</syntaxhighlight>
</lang>
<pre>
<pre>
Attributes: [16, 15, 15, 14, 8, 10]
Attributes: [16, 15, 15, 14, 8, 10]
Line 3,200: Line 3,200:


=={{header|Quackery}}==
=={{header|Quackery}}==
<lang quackery>[ 0 swap witheach + ] is sum ( [ --> n )
<syntaxhighlight lang="quackery">[ 0 swap witheach + ] is sum ( [ --> n )


[ 0 ]'[ rot witheach
[ 0 ]'[ rot witheach
Line 3,224: Line 3,224:
say 'Sum: ' dup sum echo cr
say 'Sum: ' dup sum echo cr
say '# of attributes > 14: '
say '# of attributes > 14: '
count [ 14 > ] echo</lang>
count [ 14 > ] echo</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,234: Line 3,234:


=={{header|Racket}}==
=={{header|Racket}}==
<lang racket>#lang racket
<syntaxhighlight lang="racket">#lang racket


(define (d6 . _)
(define (d6 . _)
Line 3,251: Line 3,251:
(module+ main
(module+ main
(define-values (rolled-stats total) (generate-character))
(define-values (rolled-stats total) (generate-character))
(printf "Rolls:\t~a~%Total:\t~a" rolled-stats total))</lang>
(printf "Rolls:\t~a~%Total:\t~a" rolled-stats total))</syntaxhighlight>


{{out}}
{{out}}
Line 3,259: Line 3,259:
=={{header|R}}==
=={{header|R}}==
The base library already has an attributes function, so we avoid using that name. Otherwise, this is R's bread and butter.
The base library already has an attributes function, so we avoid using that name. Otherwise, this is R's bread and butter.
<lang rsplus>genStats <- function()
<syntaxhighlight lang="rsplus">genStats <- function()
{
{
stats <- c(STR = 0, DEX = 0, CON = 0, INT = 0, WIS = 0, CHA = 0)
stats <- c(STR = 0, DEX = 0, CON = 0, INT = 0, WIS = 0, CHA = 0)
Line 3,269: Line 3,269:
if(sum(stats >= 15) < 2 || (stats["TOT"] <- sum(stats)) < 75) Recall() else stats
if(sum(stats >= 15) < 2 || (stats["TOT"] <- sum(stats)) < 75) Recall() else stats
}
}
print(genStats())</lang>
print(genStats())</syntaxhighlight>


{{out}}
{{out}}
Line 3,278: Line 3,278:
(formerly Perl 6)
(formerly Perl 6)
{{works with|Rakudo Star|2018.04.1}}
{{works with|Rakudo Star|2018.04.1}}
<lang perl6>my ( $min_sum, $hero_attr_min, $hero_count_min ) = 75, 15, 2;
<syntaxhighlight lang="raku" line>my ( $min_sum, $hero_attr_min, $hero_count_min ) = 75, 15, 2;
my @attr-names = <Str Int Wis Dex Con Cha>;
my @attr-names = <Str Int Wis Dex Con Cha>;


Line 3,291: Line 3,291:


say @attr-names Z=> @attr;
say @attr-names Z=> @attr;
say "Sum: {@attr.sum}, with {heroic(@attr)} attributes >= $hero_attr_min";</lang>
say "Sum: {@attr.sum}, with {heroic(@attr)} attributes >= $hero_attr_min";</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,299: Line 3,299:


=={{header|Red}}==
=={{header|Red}}==
<lang rebol>Red ["RPG attributes generator"]
<syntaxhighlight lang="rebol">Red ["RPG attributes generator"]


raw-attribute: does [
raw-attribute: does [
Line 3,349: Line 3,349:
]
]


show-attributes attributes</lang>
show-attributes attributes</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,365: Line 3,365:
=={{header|REXX}}==
=={{header|REXX}}==
===version 1===
===version 1===
<lang rexx>/* REXX
<syntaxhighlight lang="rexx">/* REXX
Generates 4 random, whole values between 1 and 6.
Generates 4 random, whole values between 1 and 6.
Saves the sum of the 3 largest values.
Saves the sum of the 3 largest values.
Line 3,428: Line 3,428:
swl=swl wa.i
swl=swl wa.i
End
End
Return strip(swl)</lang>
Return strip(swl)</syntaxhighlight>
{{out}}
{{out}}
<pre>I:\>rexx cast
<pre>I:\>rexx cast
Line 3,439: Line 3,439:
===version 2===
===version 2===
This REXX version doesn't need a sort to compute the sum of the largest three (of four) values.
This REXX version doesn't need a sort to compute the sum of the largest three (of four) values.
<lang rexx>/*REXX program generates values for six core attributes for a RPG (Role Playing Game).*/
<syntaxhighlight lang="rexx">/*REXX program generates values for six core attributes for a RPG (Role Playing Game).*/
do until m>=2 & $$>=75; $$= 0; list= /*do rolls until requirements are met. */
do until m>=2 & $$>=75; $$= 0; list= /*do rolls until requirements are met. */
m= 0 /*the number of values ≥ 15 (so far).*/
m= 0 /*the number of values ≥ 15 (so far).*/
Line 3,452: Line 3,452:
end /*do 6*/ /* [↑] gen six core attribute values. */
end /*do 6*/ /* [↑] gen six core attribute values. */
end /*until*/ /*stick a fork in it, we're all done. */
end /*until*/ /*stick a fork in it, we're all done. */
say 'The total for ' list " is ──► " $$', ' m " entries are ≥ 15."</lang>
say 'The total for ' list " is ──► " $$', ' m " entries are ≥ 15."</syntaxhighlight>
{{out|output|text=&nbsp; when using the default (internal) inputs:}}
{{out|output|text=&nbsp; when using the default (internal) inputs:}}
<pre>
<pre>
Line 3,460: Line 3,460:
===version 3===
===version 3===
A variation of version 2
A variation of version 2
<lang rexx>/*REXX program generates values for six core attributes for an RPG (Role Playing Game).*/
<syntaxhighlight lang="rexx">/*REXX program generates values for six core attributes for an RPG (Role Playing Game).*/
Do n=1 By 1 until m>=2 & tot>=75;
Do n=1 By 1 until m>=2 & tot>=75;
slist=''
slist=''
Line 3,479: Line 3,479:
Say 'the total for' space(slist) 'is -->' tot', 'm' entries are >= 15.'
Say 'the total for' space(slist) 'is -->' tot', 'm' entries are >= 15.'
end
end
Say 'Solution found with' n 'iterations'</lang>
Say 'Solution found with' n 'iterations'</syntaxhighlight>
{{out}}
{{out}}
<pre>I:\>rexx rpg
<pre>I:\>rexx rpg
Line 3,488: Line 3,488:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
# Project : RPG Attributes Generator
# Project : RPG Attributes Generator


Line 3,533: Line 3,533:
line = line + "])"
line = line + "])"
see line + nl
see line + nl
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 3,540: Line 3,540:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>res = []
<syntaxhighlight lang="ruby">res = []
until res.sum >= 75 && res.count{|n| n >= 15} >= 2 do
until res.sum >= 75 && res.count{|n| n >= 15} >= 2 do
res = Array.new(6) do
res = Array.new(6) do
Line 3,550: Line 3,550:
p res
p res
puts "sum: #{res.sum}"
puts "sum: #{res.sum}"
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>[12, 14, 17, 12, 16, 9]
<pre>[12, 14, 17, 12, 16, 9]
Line 3,557: Line 3,557:


=={{header|Run BASIC}}==
=={{header|Run BASIC}}==
<lang runbasic>dim statnames$(6)
<syntaxhighlight lang="runbasic">dim statnames$(6)
data "STR", "CON", "DEX", "INT", "WIS", "CHA"
data "STR", "CON", "DEX", "INT", "WIS", "CHA"
for i = 1 to 6
for i = 1 to 6
Line 3,589: Line 3,589:
a = d6() : b = d6() : c = d6() : d = d6()
a = d6() : b = d6() : c = d6() : d = d6()
rollstat = a + b + c + d - min(min(a, b), min(c, d))
rollstat = a + b + c + d - min(min(a, b), min(c, d))
end function</lang>
end function</syntaxhighlight>


=={{header|Rust}}==
=={{header|Rust}}==
Line 3,595: Line 3,595:
{{libheader|rand}}
{{libheader|rand}}
{{works with|Rust|2018}}
{{works with|Rust|2018}}
<lang rust>
<syntaxhighlight lang="rust">
use rand::distributions::Uniform;
use rand::distributions::Uniform;
use rand::prelude::{thread_rng, ThreadRng};
use rand::prelude::{thread_rng, ThreadRng};
Line 3,672: Line 3,672:
}
}
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
Sample output, running the generator ten times:
Sample output, running the generator ten times:
Line 3,690: Line 3,690:


=={{header|Scala}}==
=={{header|Scala}}==
<lang scala>
<syntaxhighlight lang="scala">
import scala.util.Random
import scala.util.Random
Random.setSeed(1)
Random.setSeed(1)
Line 3,711: Line 3,711:


println("picked => " + getCharacter.mkString("[", ",", "]"))
println("picked => " + getCharacter.mkString("[", ",", "]"))
</syntaxhighlight>
</lang>


<pre>
<pre>
Line 3,722: Line 3,722:


=={{header|Seed7}}==
=={{header|Seed7}}==
<lang seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";


const proc: main is func
const proc: main is func
Line 3,759: Line 3,759:
writeln(" ----");
writeln(" ----");
writeln("Total " <& total lpad 3);
writeln("Total " <& total lpad 3);
end func;</lang>
end func;</syntaxhighlight>


{{out}}
{{out}}
Line 3,777: Line 3,777:
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
{{works with|QBasic}}
{{works with|QBasic}}
<lang qbasic>FUNCTION min(a, b)
<syntaxhighlight lang="qbasic">FUNCTION min(a, b)
IF a < b THEN LET min = a ELSE LET min = b
IF a < b THEN LET min = a ELSE LET min = b
END FUNCTION
END FUNCTION
Line 3,818: Line 3,818:
PRINT "--------"
PRINT "--------"
PRINT "TOT: "; sum
PRINT "TOT: "; sum
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>Igual que la entrada de FreeBASIC.</pre>
<pre>Igual que la entrada de FreeBASIC.</pre>
Line 3,824: Line 3,824:
=={{header|uBasic/4tH}}==
=={{header|uBasic/4tH}}==
{{trans|Yabasic}}
{{trans|Yabasic}}
<lang>dim @n(6)
<syntaxhighlight lang="text">dim @n(6)
@n(0) := "STR"
@n(0) := "STR"
@n(1) := "CON"
@n(1) := "CON"
Line 3,857: Line 3,857:
local (4)
local (4)
a@ = FUNC(_d6) : b@ = FUNC(_d6) : c@ = FUNC(_d6) : d@ = FUNC(_d6)
a@ = FUNC(_d6) : b@ = FUNC(_d6) : c@ = FUNC(_d6) : d@ = FUNC(_d6)
return (a@ + b@ + c@ + d@ - Min(Min(a@, b@), Min(c@, d@)))</lang>
return (a@ + b@ + c@ + d@ - Min(Min(a@, b@), Min(c@, d@)))</syntaxhighlight>
{{Out}}
{{Out}}
<pre>STR: 17
<pre>STR: 17
Line 3,874: Line 3,874:
{{works with|Zsh}}
{{works with|Zsh}}


<lang sh>function main {
<syntaxhighlight lang="sh">function main {
typeset attrs=(str dex con int wis cha)
typeset attrs=(str dex con int wis cha)
typeset -A values
typeset -A values
Line 3,910: Line 3,910:


main "$@"
main "$@"
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>str: 12
<pre>str: 12
Line 3,921: Line 3,921:
=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
repeats until a successful outcome occurs
repeats until a successful outcome occurs
<lang vbnet>Module Module1
<syntaxhighlight lang="vbnet">Module Module1


Dim r As New Random
Dim r As New Random
Line 3,946: Line 3,946:
Loop Until good
Loop Until good
End Sub
End Sub
End Module</lang>
End Module</syntaxhighlight>
{{out}}
{{out}}
sample outputs:
sample outputs:
Line 3,963: Line 3,963:
=={{header|Wren}}==
=={{header|Wren}}==
{{libheader|Wren-sort}}
{{libheader|Wren-sort}}
<lang ecmascript>import "random" for Random
<syntaxhighlight lang="ecmascript">import "random" for Random
import "/sort" for Sort
import "/sort" for Sort


Line 3,985: Line 3,985:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 3,995: Line 3,995:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>func Gen; \Return sum of the three largest of four random values
<syntaxhighlight lang="xpl0">func Gen; \Return sum of the three largest of four random values
int I, R, Min, SI, Sum, Die(4);
int I, R, Min, SI, Sum, Die(4);
[Min:= 7; Sum:= 0;
[Min:= 7; Sum:= 0;
Line 4,020: Line 4,020:
[IntOut(0, Value(J)); ChOut(0, ^ )];
[IntOut(0, Value(J)); ChOut(0, ^ )];
CrLf(0);
CrLf(0);
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 4,030: Line 4,030:
=={{header|Yabasic}}==
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang freebasic>sub d6()
<syntaxhighlight lang="freebasic">sub d6()
//simulates a marked regular hexahedron coming to rest on a plane
//simulates a marked regular hexahedron coming to rest on a plane
return 1 + int(ran(6))
return 1 + int(ran(6))
Line 4,067: Line 4,067:
next i
next i
print "-------\nTOT: ", sum
print "-------\nTOT: ", sum
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>Igual que la entrada de FreeBASIC.</pre>
<pre>Igual que la entrada de FreeBASIC.</pre>


=={{header|zkl}}==
=={{header|zkl}}==
<lang zkl>reg attrs=List(), S,N;
<syntaxhighlight lang="zkl">reg attrs=List(), S,N;
do{
do{
attrs.clear();
attrs.clear();
Line 4,081: Line 4,081:
}while((S=attrs.sum(0))<75 or (N=attrs.filter('>=(15)).len())<2);
}while((S=attrs.sum(0))<75 or (N=attrs.filter('>=(15)).len())<2);
println("Random numbers: %s\nSums to %d, with %d >= 15"
println("Random numbers: %s\nSums to %d, with %d >= 15"
.fmt(attrs.concat(","),S,N));</lang>
.fmt(attrs.concat(","),S,N));</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>