RPG attributes generator: Difference between revisions

Added Easylang
(Add Red)
(Added Easylang)
 
(20 intermediate revisions by 15 users not shown)
Line 36:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">random:seed(Int(Time().unix_time()))
V total = 0
V count = 0
Line 50:
total = sum(attributes)
 
print(total‘ ’attributes)</langsyntaxhighlight>
 
{{out}}
Line 59:
=={{header|8086 Assembly}}==
 
<langsyntaxhighlight lang="asm"> bits 16
cpu 8086
putch: equ 2h
Line 147:
section .bss
rnddat: resb 4 ; RNG state
attrs: resb 6 ; Rolled attributes</langsyntaxhighlight>
 
{{out}}
Line 162:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program rpg64.s */
Line 398:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
{{Output}}
<pre>
Line 413:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">TYPE Result=[BYTE success,sum,highCount]
BYTE FUNC GenerateAttrib()
BYTE i,v,min,sum
Line 473:
FI
OD
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/RPG_attributes_generator.png Screenshot from Atari 8-bit computer]
Line 485:
=={{header|ALGOL 68}}==
{{Trans|Action!}}
<langsyntaxhighlight lang="algol68">BEGIN # RPG attributes generator #
 
INT attrib count = 6;
Line 537:
OD
 
END</langsyntaxhighlight>
{{out}}
<pre>
Line 549:
{{works with|Dyalog APL}}
 
<langsyntaxhighlight APLlang="apl">roll←{(+/-⌊/)¨?¨6/⊂4/6}⍣{(75≤+/⍺)∧2≤+/⍺≥15}</langsyntaxhighlight>
 
{{out}}
Line 564:
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
 
/* ARM assembly Raspberry PI */
Line 888:
bx lr
 
</syntaxhighlight>
</lang>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">vals: []
 
while [or? 75 > sum vals
Line 906:
 
print ["values:" vals ]
print ["with sum:" sum vals]</langsyntaxhighlight>
 
=={{header|Atari BASIC}}==
{{trans|Commodore BASIC}}
<langsyntaxhighlight lang="basic">100 REM RPG character generator
110 DIM AT(5)
120 DIM AT$(18)
Line 948:
450 IF K=78 THEN 140
460 POSITION 0,13
470 PRINT "Excellent. Good luck on your adventure!"</langsyntaxhighlight>
 
{{Out}}
Line 971:
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="freebasic">function min(a, b)
if a < b then return a else return b
end function
Line 1,006:
print "-------"
print "TOT: "; sum
end</langsyntaxhighlight>
{{out}}
<pre>Igual que la entrada de FreeBASIC.</pre>
 
=={{header|BQN}}==
Slightly different from the APL solution primarily due to builtin differences.
<syntaxhighlight lang="bqn"> _while_←{𝔽⍟𝔾∘𝔽_𝕣_𝔾∘𝔽⍟𝔾𝕩}
(2-modifier block)
Roll←{𝕊:{𝕊:((+´-⌊´)1+4⊸•rand.Range)¨6⥊6}_while_{(75>+´𝕩)∨2>+´15≤𝕩}⟨⟩}
(function block)
Roll@
⟨ 11 16 12 10 16 13 ⟩</syntaxhighlight>
=={{header|C}}==
{{trans|Go}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <time.h>
Line 1,053 ⟶ 1,061:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 1,066 ⟶ 1,074:
=={{header|C sharp|C#}}==
{{trans|Visual Basic .NET}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 1,097 ⟶ 1,105:
} while (!good);
}
}</langsyntaxhighlight>
{{out}}
sample outputs:
Line 1,106 ⟶ 1,114:
=={{header|C++}}==
GCC 4.9.2, unoptimised.
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <ctime>
#include <iostream>
Line 1,157 ⟶ 1,165:
return 0;
}</langsyntaxhighlight>
{{out}}
Sample run:
Line 1,166 ⟶ 1,174:
 
=={{header|Caché ObjectScript}}==
<syntaxhighlight lang="caché objectscript">RPGGEN
<lang Caché ObjectScript>RPGGEN
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.",!
Line 1,211 ⟶ 1,219:
} while (result '= 3)
quit</langsyntaxhighlight>
{{out}}<pre>
Line 1,227 ⟶ 1,235:
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">% This program needs to be merged with PCLU's "misc" library
% to use the random number generator.
%
Line 1,296 ⟶ 1,304:
stream$putright(po, int$unparse(stat), 4)
end
end start_up</langsyntaxhighlight>
{{out}}
<pre>$ ./rpg_gen
Line 1,312 ⟶ 1,320:
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.)
 
<langsyntaxhighlight lang="gwbasic">100 rem rpg character roller
110 rem rosetta code - commodore basic
120 dim di(3):rem dice
Line 1,351 ⟶ 1,359:
470 if di(x)<di(x+1) then t=di(x):di(x)=di(x+1):di(x+1)=t
480 next x
490 return</langsyntaxhighlight>
 
{{out}}
Line 1,395 ⟶ 1,403:
 
=={{header|Common Lisp}}==
===Mapping functions===
<lang lisp>
<syntaxhighlight lang="lisp">
(defpackage :rpg-generator
(:use :cl)
Line 1,414 ⟶ 1,423:
(format t ">= 15: ~A~%" (count-if (lambda (n) (>= n 15)) scores))
(return scores)))
</syntaxhighlight>
</lang>
=== Loop macro ===
 
''Draft''
 
==== Note ====
 
See [https://gigamonkeys.com/book/loop-for-black-belts.html Loop for Black Belts ]
 
==== Program ====
 
<syntaxhighlight lang="lisp">;; 22.11.07 Draft
 
(defun score-jet-des ()
(loop :for resultat = (+ (random 6) 1)
:repeat 4
:sum resultat :into total
:minimize resultat :into minimum
:finally (return (- total minimum))))
 
(defun calcule-attributs-personnage ()
(loop named a
:do (loop :for score = (score-jet-des)
:repeat 6
:collect score :into scores
:sum score :into total
:count (>= score 15) :into frequence
:finally (when (and (>= total 75) (>= frequence 2))
(return-from a (values scores total))))))
</syntaxhighlight>
 
==== Execution ====
 
<pre>
(multiple-value-bind (scores total)
(calcule-attributs-personnage)
(list scores total))
</pre>
 
{{out}}
<pre>
((16 9 15 12 14 14) 80)
</pre>
 
''cyril nocton (cyril.nocton@gmail.com) w/ google translate''
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
include "argv.coh";
 
Line 1,513 ⟶ 1,566:
print("\nTotal: ");
print_i8(total);
print_nl();</langsyntaxhighlight>
 
{{out}}
Line 1,528 ⟶ 1,581:
 
=={{header|Crystal}}==
<langsyntaxhighlight Rubylang="ruby">def roll_stat
dices = Array(Int32).new(4) { rand(1..6) }
dices.sum - dices.min
Line 1,543 ⟶ 1,596:
stats = roll_character
puts "stats: #{stats}, sum is #{stats.sum}"
end</langsyntaxhighlight>
 
sample output:
Line 1,561 ⟶ 1,614:
{{libheader| System.Generics.Collections}}
{{Trans|C#}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program RPG_Attributes_Generator;
 
Line 1,675 ⟶ 1,728:
end.
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,684 ⟶ 1,737:
=={{header|Dyalect}}==
{{trans|C#}}
<langsyntaxhighlight lang="dyalect">func getThree(n) {
var g3 = []
for i in 0..33 {
g3.addAdd(rnd(max: n) + 1)
}
g3.sortSort()
g3.removeAtRemoveAt(0)
g3
}
Line 1,697 ⟶ 1,750:
var g6 = []
for i in 0..5 {
g6.addAdd(getThree(6).sumSum())
}
g6
}
func Array.sumSum() {
var acc = 0
for x in this {
Line 1,710 ⟶ 1,763:
}
func Array.findAllFindAll(pred) {
for x in this when pred(x) {
yield x
Line 1,720 ⟶ 1,773:
while !good {
var gs = getSix()
var gss = gs.sumSum()
var hvc = gs.findAllFindAll(x => x > 14).lenLength()
print("attribs: \(gs), sum=\(gss), ", terminator: "")
let gl = gss >= 75 ? "good" : "low"
print("(\(gl) sum, high vals=\(hvc))", terminator: "")
good = gs.sumSum() >= 75 && hvc > 1
print(" - " + (good ? "success" : "failure"))
}</langsyntaxhighlight>
 
{{out}}
Line 1,734 ⟶ 1,787:
 
=={{header|EasyLang}}==
{{trans|BASIC256}}
 
<syntaxhighlight>
<lang>len v[] 6
func rollstat .
for i to 4
h = randint 6
s += h
min = lower min h
.
return s - min
.
state$[] = [ "STR" "CON" "DEX" "INT" "WIS" "CHA" ]
len stat[] 6
repeat
vsum sum = 0
vmin n15 = 0
for i rangeto 6
val stat[i] = 0rollstat
min sum += 6stat[i]
for j rangeif 4stat[i] >= 15
h = random 6n15 += 1
val += h
if h < min
min = h
.
.
until valsum ->= min75 and n15 >= 2
v[i] = val
if val >= 15
vmin += 1
.
vsum += val
.
until vsum >= 75 and vmin >= 2
.
for i to 6
print "Attributes: " & " " & v[]
print "Total: " print state$[i] & ": " & vsum</lang>stat[i]
.
print "-------"
print "TOT: " & sum
</syntaxhighlight>
 
{{out}}
<pre>
STR: 13
Attributes: [ 18 13 17 15 9 11 ]
CON: 11
Total: 83
DEX: 16
INT: 8
WIS: 17
CHA: 18
-------
TOT: 83
</pre>
 
=={{header|Factor}}==
{{Works with|Factor|0.98}}
<langsyntaxhighlight lang="factor">USING: combinators.short-circuit dice formatting io kernel math
math.statistics qw sequences ;
IN: rosetta-code.rpg-attributes-generator
Line 1,793 ⟶ 1,857:
[ "%s: %d\n" printf ] 2each nl stats-info ;
MAIN: main</langsyntaxhighlight>
{{out}}
<pre>
Line 1,808 ⟶ 1,872:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#define min(a, b) iif(a < b, a, b)
 
function d6() as integer
Line 1,841 ⟶ 1,905:
next i
print "--------"
print using "TOT: ##";sum</langsyntaxhighlight>
{{out}}<pre>STR: 14
CON: 11
Line 1,853 ⟶ 1,917:
 
=={{header|FOCAL}}==
<langsyntaxhighlight FOCALlang="focal">01.10 S T=0
01.20 F X=1,6;D 4;S AT(X)=S3;S T=T+S3
01.30 I (T-75)1.1
Line 1,876 ⟶ 1,940:
06.01 C--ROLL A D6
06.10 S A=FRAN()*10;S A=A-FITR(A)
06.20 S A=1+FITR(A*6)</langsyntaxhighlight>
 
{{out}}
Line 1,902 ⟶ 1,966:
{{works with|GNU Forth}}
{{libheader|random.fs}}
<langsyntaxhighlight lang="forth">require random.fs
: d6 ( -- roll ) 6 random 1 + ;
 
Line 1,937 ⟶ 2,001:
;
 
utime drop seed !</langsyntaxhighlight>
 
{{Out}}
<pre>roll str:13 dex:15 con:14 int:8 wis:17 cha:10 (total:77) ok</pre>
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
_elements = 6
 
local fn min( a as long, b as long ) as long
long result
if ( a < b )
result = a : exit fn
else
result = b : exit fn
end if
end fn = result
 
local fn d6 as long
long result
result = 1 + int( rnd(_elements) )
end fn = result
 
local fn roll_stat as long
long result
long a = fn d6, b = fn d6, c = fn d6, d = fn d6
result = a + b + c + d - fn min( fn min( a, b ), fn min( c, d ) )
end fn = result
 
local fn DoIt
CFArrayRef statnames = @[@"Strength",@"Constitution",@"Dexterity",@"Intelligence",@"Wisdom",@"Charisma"]
long stat(_elements), n15, sum, i
BOOL acceptable = NO
randomize
do
sum = 0
n15 = 0
for i = 1 to _elements
stat(i) = fn roll_stat
sum = sum + stat(i)
if stat(i) >= 15 then n15++
next
if sum >= 75 and n15 >= 2 then acceptable = YES
until ( acceptable = YES )
for i = 1 to _elements
printf @"%12s %3ld", fn StringUTF8String( statnames[i -1] ), stat(i)
next
printf @"------------"
printf @"%13s %3ld", "Total:", sum
end fn
 
fn DoIt
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
Strength 17
Constitution 11
Dexterity 18
Intelligence 12
Wisdom 19
Charisma 11
------------
Total: 88
</pre>
 
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,989 ⟶ 2,118:
break
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,001 ⟶ 2,130:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Control.Monad (replicateM)
import System.Random (randomRIO)
import Data.Bool (bool)
Line 2,019 ⟶ 2,148:
-------------------------- TEST ---------------------------
main :: IO ()
main = replicateM 10 character >>= mapM_ (print . (sum >>= (,)))</langsyntaxhighlight>
{{Out}}
<pre>Sample computation:
Line 2,036 ⟶ 2,165:
=={{header|J}}==
'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:
massage=: +/ - <./
generate_attributes=: massage@:roll
Line 2,049 ⟶ 2,178:
NB. use: generate_character 'name'
generate_character=: (; (+/ ; ])@:([: generate_attributes@:show Until accept 0:))&>@:boxopen
</syntaxhighlight>
</lang>
 
 
Line 2,082 ⟶ 2,211:
 
=={{header|Java}}==
<langsyntaxhighlight Javalang="java">import java.util.List;
import java.util.Random;
import java.util.stream.Stream;
Line 2,115 ⟶ 2,244:
}
}
}</langsyntaxhighlight>
{{Out}}
<pre>
Line 2,124 ⟶ 2,253:
=={{header|Javascript}}==
===Imperative===
<langsyntaxhighlight lang="javascript">function roll() {
const stats = {
total: 0,
Line 2,157 ⟶ 2,286:
${rolledCharacter.rolls.join(', ')}
 
Their sum is ${rolledCharacter.total} and ${rolledCharacter.rolls.filter(a => a >= 15).length} of them are >= 15`);</langsyntaxhighlight>
 
{{out}}
Line 2,170 ⟶ 2,299:
===Functional===
{{Trans|Python}} (Functional composition version)
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 2,293 ⟶ 2,422:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
A sample of 10 character attribute sets:
Line 2,306 ⟶ 2,435:
79 -> [15,15,11,17,12,9]
76 -> [14,12,9,15,15,11]</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq.'''
 
In this entry, /dev/random is used as a source of entropy,
via the invocation:
<pre>
< /dev/random tr -cd '0-9' | fold -w 1 | jq -Mcnr -f rgp-attributes.jq
</pre>
where rgp-attributes.jq is a file containing the jq program shown below.
 
'''The jq program'''
<syntaxhighlight lang=jq>
def count(s): reduce s as $x (0; .+1);
 
# Output: a PRN in range(0;$n) where $n is .
def prn:
if . == 1 then 0
else . as $n
| (($n-1)|tostring|length) as $w
| [limit($w; inputs)] | join("") | tonumber
| if . < $n then . else ($n | prn) end
end;
 
# Output: [$four, $sum]
# where $four is an array of 4 pseudo-random integers between 1 and 6 inclusive,
# and $sum records the sum of the 3 largest values.
def generate_RPG:
[range(0; 4) | (1 + (7|prn) )] as $four
| [$four, ($four|sort|.[-3:]|add)];
 
# Input: $six as produced by [range(0;6) | generate_RPG]
# Determine if the following conditions are met:
# - the total of all 6 of the values at .[-1] is at least 75;
# - at least 2 of these values must be 15 or more.
def ok:
([.[][-1]] | add) as $sum
| $sum >= 75 and
count( (.[][1] >= 15) // empty) >= 2;
 
# First show [range(0;6) | generate_RPG]
# and then determine if it meets the "ok" condition;
# if not, repeat until a solution has been found.
def task:
[range(0;6) | generate_RPG] as $six
| ([$six[][-1]] | add) as $sum
| $six[], "Sum: \($sum)",
if $six | ok then "All done."
else "continuing search ...",
({}
| until(.emit;
[range(0;6) | generate_RPG] as $six
| ([$six[][-1]] | add) as $sum
| if $six | ok
then .emit = {$six, $sum}
else .
end).emit
| (.six[], "Sum: \(.sum)" ) )
end;
 
task
</syntaxhighlight>
{{Output}}
''Example of a run which only one round of throwing the four dice''
<pre>
[[7,7,1,1],15]
[[1,5,3,7],15]
[[5,3,2,1],10]
[[2,4,7,5],16]
[[7,4,2,7],18]
[[3,6,2,4],13]
Sum: 87
All done.
</pre>
''Example of a run requiring more than one round of throwing the four dice''
<pre>
[[6,6,1,3],15]
[[3,3,6,5],14]
[[7,2,7,5],19]
[[6,6,5,5],17]
[[5,7,3,4],16]
[[6,1,2,5],13]
Sum: 94
continuing search ...
[[7,7,2,7],21]
[[7,4,1,6],17]
[[7,3,3,1],13]
[[7,7,6,4],20]
[[2,3,6,5],14]
[[6,1,5,2],13]]
Sum: 98
</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">roll_skip_lowest(dice, sides) = (r = rand(collect(1:sides), dice); sum(r) - minimum(r))
 
function rollRPGtoon()
Line 2,329 ⟶ 2,551:
rollRPGtoon()
rollRPGtoon()
</langsyntaxhighlight>{{output}}<pre>
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.
Line 2,336 ⟶ 2,558:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="kotlin">import kotlin.random.Random
 
fun main() {
Line 2,351 ⟶ 2,573:
break
}
}</langsyntaxhighlight>
 
{{output}}
Line 2,361 ⟶ 2,583:
 
=={{header|Ksh}}==
<langsyntaxhighlight lang="ksh">
#!/bin/ksh
 
Line 2,432 ⟶ 2,654:
print "Attribute value total: ${total}"
print "Attribule count >= 15: ${cnt15}"
</syntaxhighlight>
</lang>
{{out}}<pre>
strength: 11
Line 2,442 ⟶ 2,664:
Attribute value total: 81
Attribule count >= 15: 2 </pre>
 
=={{header|LDPL}}==
<syntaxhighlight lang="ldpl">data:
attributes is number list
i is number
attr is number
sum is number
count is number
 
procedure:
sub attribute
parameters:
result is number
local data:
min is number
n is number
i is number
procedure:
store 6 in min
for i from 0 to 4 step 1 do
get random in n
in n solve 6 * n + 1
floor n
add n and result in result
if n is less than min then
store n in min
end if
repeat
subtract min from result in result
end sub
create statement "get attribute in $" executing attribute
 
label generate-attributes
for i from 0 to 6 step 1 do
get attribute in attr
add attr and sum in sum
if attr is greater than 14 then
add count and 1 in count
end if
push attr to attributes
store 0 in attr
repeat
 
if sum is less than 75 or count is less than 2 then
display "Failed..." lf
store 0 in sum
store 0 in count
clear attributes
goto generate-attributes
end if
 
for each attr in attributes do
display attr " "
repeat
display lf "Sum: " sum lf ">14: " count lf
</syntaxhighlight>
{{out}}
<pre>
Failed...
Failed...
Failed...
Failed...
11 15 12 16 15 15
Sum: 84
>14: 4
</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">valid = False;
While[! valid,
try = Map[Total[TakeLargest[#, 3]] &,
Line 2,452 ⟶ 2,740:
]
]
{Total[try], try}</langsyntaxhighlight>
{{out}}
<pre>{78, {13, 15, 9, 13, 12, 16}}</pre>
Line 2,458 ⟶ 2,746:
=={{header|min}}==
{{works with|min|0.19.6}}
<langsyntaxhighlight lang="min">randomize ; Seed the rng with current timestamp.
 
; Implement some general operators we'll need that aren't in the library.
Line 2,474 ⟶ 2,762:
(big (dup special?) () (pop big) () linrec) :stats ; Roll a set of big and special attributes.
 
stats puts "Total: " print! sum puts!</langsyntaxhighlight>
{{out}}
<pre>
Line 2,482 ⟶ 2,770:
 
=={{header|MiniScript}}==
<langsyntaxhighlight MiniScriptlang="miniscript">roll = function()
results = []
for i in range(0,3)
Line 2,508 ⟶ 2,796:
end while
print "Success!"
</syntaxhighlight>
</lang>
{{out}}
<pre>Attribute values: 11, 13, 8, 10, 8, 10
Line 2,527 ⟶ 2,815:
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">
<lang Nim>
# Import "random" to get random numbers and "algorithm" to get sorting functions for arrays.
import random, algorithm
Line 2,561 ⟶ 2,849:
if not twoFifteens(sixAttr) or sumAttr < 75: echo "Not good enough. Rerolling..."
else: break
</syntaxhighlight>
</lang>
 
Sample output: <pre>
Line 2,572 ⟶ 2,860:
=={{header|OCaml}}==
Original version by [http://rosettacode.org/wiki/User:Vanyamil User:Vanyamil]
<syntaxhighlight lang="ocaml">
<lang OCaml>
(* Task : RPG_attributes_generator *)
 
Line 2,608 ⟶ 2,896:
let s = valid_set () in
List.iter (fun i -> print_int i; print_string ", ") s
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,616 ⟶ 2,904:
=={{header|Pascal|FreePascal}}==
 
<syntaxhighlight lang="pascal">
<lang Pascal>
program attributes;
 
Line 2,656 ⟶ 2,944:
writeln(' ---');
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,691 ⟶ 2,979:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use List::Util 'sum';
 
Line 2,710 ⟶ 2,998:
 
printf "%s = %2d\n", $attr_names[$_], $attr[$_] for 0..$#attr;
printf "Sum = %d, with %d attributes >= $hero_attr_min\n", sum(@attr), heroic(@attr);</langsyntaxhighlight>
{{out}}
<pre>Str = 13
Line 2,721 ⟶ 3,009:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>sequence numbers = repeat(0,6)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
integer t,n
<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>
while true do
<span style="color: #004080;">integer</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span>
for i=1 to length(numbers) do
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span>
sequence ni = sq_rand(repeat(6,4))
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">numbers</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
numbers[i] = sum(ni)-min(ni)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">ni</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_rand</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">))</span>
end for
<span style="color: #000000;">numbers</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ni</span><span style="color: #0000FF;">)-</span><span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ni</span><span style="color: #0000FF;">)</span>
t = sum(numbers)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
n = sum(sq_ge(numbers,15))
<span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">numbers</span><span style="color: #0000FF;">)</span>
if t>=75 and n>=2 then exit end if
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_ge</span><span style="color: #0000FF;">(</span><span style="color: #000000;">numbers</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">))</span>
?"re-rolling..." -- (occasionally >20)
<span style="color: #008080;">if</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">75</span> <span style="color: #008080;">and</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end while
<span style="color: #0000FF;">?</span><span style="color: #008000;">"re-rolling..."</span> <span style="color: #000080;font-style:italic;">-- (occasionally &gt;20)</span>
printf(1,"The 6 attributes generated are:\n")
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
printf(1,"strength %d, dexterity %d, constitution %d, "&
<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;">"The 6 attributes generated are:\n"</span><span style="color: #0000FF;">)</span>
"intelligence %d, wisdom %d, and charisma %d.\n",
<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;">"strength %d, dexterity %d, constitution %d, "</span><span style="color: #0000FF;">&</span>
numbers)
<span style="color: #008000;">"intelligence %d, wisdom %d, and charisma %d.\n"</span><span style="color: #0000FF;">,</span>
printf(1,"\nTheir sum is %d and %d of them are >=15\n",{t,n})</lang>
<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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 2,747 ⟶ 3,038:
Their sum is 86 and 3 of them are >=15
</pre>
 
=={{header|Phixmonti}}==
{{trans|BASIC256}}
<syntaxhighlight lang="Phixmonti">/# Rosetta Code problem: https://rosettacode.org/wiki/RPG_attributes_generator
by Galileo, 11/2022 #/
 
include ..\Utilitys.pmt
 
def d6 rand 6 * int 1 + enddef
 
( "STR" "CON" "DEX" "INT" "WIS" "CHA" )
 
true while
0 0
6 for drop 0 >ps
( d6 d6 d6 d6 ) len for get ps> + >ps endfor
min ps> swap -
dup >ps +
tps 15 > if swap 1 + swap endif
endfor
75 >= swap 2 >= and not
endwhile
 
0 swap
6 for
get print ": " print swap tps + swap ps> ?
endfor
 
drop "-------" ? "TOT: " print ?</syntaxhighlight>
{{out}}
<pre>STR: 14
CON: 9
DEX: 17
INT: 16
WIS: 13
CHA: 15
-------
TOT: 84
 
=== Press any key to exit ===</pre>
 
=={{header|PHP}}==
===Version 1===
<langsyntaxhighlight lang="php"><?php
 
$attributesTotal = 0;
Line 2,780 ⟶ 3,111:
}
 
print_r($attributes);</langsyntaxhighlight>
 
===Version 2===
<langsyntaxhighlight lang="php"><?php
class CharacterGenerator {
Line 2,822 ⟶ 3,153:
print "Attribute Total: $attributesTotal\n";
print "Attributes: $attributesString";</langsyntaxhighlight>
 
{{out}}
Line 2,829 ⟶ 3,160:
 
=={{header|Plain English}}==
<langsyntaxhighlight lang="plainenglish">To add an attribute to some stats:
Allocate memory for an entry.
Put the attribute into the entry's attribute.
Line 2,908 ⟶ 3,239:
If the entry's next is not nil, write ", " on the console without advancing.
Put the entry's next into the entry.
Repeat.</langsyntaxhighlight>
{{out}}
<pre>
Line 2,917 ⟶ 3,248:
 
=={{header|PureBasic}}==
<langsyntaxhighlight lang="purebasic">#heroicAttributeMinimum = 15
#heroicAttributeCountMinimum = 2
#attributeSumMinimum = 75
Line 2,973 ⟶ 3,304:
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf</langsyntaxhighlight>
Sample output:
<pre>Attributes generated: [13, 17, 17, 11, 9, 17]
Line 2,980 ⟶ 3,311:
=={{header|Python}}==
===Python: Simple===
<langsyntaxhighlight lang="python">import random
random.seed()
attributes_total = 0
Line 3,006 ⟶ 3,337:
attributes_total = sum(attributes)
print(attributes_total, attributes)</langsyntaxhighlight>
 
{{out}}
Line 3,013 ⟶ 3,344:
 
===Python: Nested Comprehensions #1===
<langsyntaxhighlight lang="python">import random
random.seed()
total = 0
Line 3,027 ⟶ 3,358:
total = sum(attributes)
print(total, attributes)</langsyntaxhighlight>
 
{{out}}
Line 3,035 ⟶ 3,366:
===Python: Nested Comprehensions #2===
With comprehensions for checking candidate values in the while expression.
<langsyntaxhighlight lang="python">import random
 
def compute():
Line 3,046 ⟶ 3,377:
for i in range(3):
print(*compute())
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,056 ⟶ 3,387:
Composing a hero-generator from reusable functions:
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''RPG Attributes Generator'''
 
from itertools import islice
Line 3,172 ⟶ 3,503:
 
if __name__ == '__main__':
main()</langsyntaxhighlight>
<pre>A sample of 10:
 
Line 3,188 ⟶ 3,519:
===Python: One-liner===
Just because you can, doesn't mean you should.
<langsyntaxhighlight 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)])))
</syntaxhighlight>
</lang>
<pre>
Attributes: [16, 15, 15, 14, 8, 10]
Line 3,197 ⟶ 3,528:
 
=={{header|Quackery}}==
<langsyntaxhighlight lang="quackery">[ 0 swap witheach + ] is sum ( [ --> n )
 
[ 0 ]'[ rot witheach
Line 3,221 ⟶ 3,552:
say 'Sum: ' dup sum echo cr
say '# of attributes > 14: '
count [ 14 > ] echo</langsyntaxhighlight>
{{out}}
<pre>
Line 3,231 ⟶ 3,562:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
 
(define (d6 . _)
Line 3,248 ⟶ 3,579:
(module+ main
(define-values (rolled-stats total) (generate-character))
(printf "Rolls:\t~a~%Total:\t~a" rolled-stats total))</langsyntaxhighlight>
 
{{out}}
Line 3,256 ⟶ 3,587:
=={{header|R}}==
The base library already has an attributes function, so we avoid using that name. Otherwise, this is R's bread and butter.
<langsyntaxhighlight lang="rsplus">genStats <- function()
{
stats <- c(STR = 0, DEX = 0, CON = 0, INT = 0, WIS = 0, CHA = 0)
Line 3,266 ⟶ 3,597:
if(sum(stats >= 15) < 2 || (stats["TOT"] <- sum(stats)) < 75) Recall() else stats
}
print(genStats())</langsyntaxhighlight>
 
{{out}}
Line 3,275 ⟶ 3,606:
(formerly Perl 6)
{{works with|Rakudo Star|2018.04.1}}
<syntaxhighlight lang="raku" perl6line>my ( $min_sum, $hero_attr_min, $hero_count_min ) = 75, 15, 2;
my @attr-names = <Str Int Wis Dex Con Cha>;
 
Line 3,288 ⟶ 3,619:
 
say @attr-names Z=> @attr;
say "Sum: {@attr.sum}, with {heroic(@attr)} attributes >= $hero_attr_min";</langsyntaxhighlight>
{{out}}
<pre>
Line 3,296 ⟶ 3,627:
 
=={{header|Red}}==
<langsyntaxhighlight lang="rebol">Red ["RPG attributes generator"]
 
raw-attribute: does [
Line 3,346 ⟶ 3,677:
]
 
show-attributes attributes</langsyntaxhighlight>
{{out}}
<pre>
Line 3,362 ⟶ 3,693:
=={{header|REXX}}==
===version 1===
<langsyntaxhighlight lang="rexx">/* REXX
Generates 4 random, whole values between 1 and 6.
Saves the sum of the 3 largest values.
Line 3,425 ⟶ 3,756:
swl=swl wa.i
End
Return strip(swl)</langsyntaxhighlight>
{{out}}
<pre>I:\>rexx cast
Line 3,436 ⟶ 3,767:
===version 2===
This REXX version doesn't need a sort to compute the sum of the largest three (of four) values.
<langsyntaxhighlight 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. */
m= 0 /*the number of values ≥ 15 (so far).*/
Line 3,449 ⟶ 3,780:
end /*do 6*/ /* [↑] gen six core attribute values. */
end /*until*/ /*stick a fork in it, we're all done. */
say 'The total for ' list " is ──► " $$', ' m " entries are ≥ 15."</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default (internal) inputs:}}
<pre>
Line 3,457 ⟶ 3,788:
===version 3===
A variation of version 2
<langsyntaxhighlight 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;
slist=''
Line 3,476 ⟶ 3,807:
Say 'the total for' space(slist) 'is -->' tot', 'm' entries are >= 15.'
end
Say 'Solution found with' n 'iterations'</langsyntaxhighlight>
{{out}}
<pre>I:\>rexx rpg
Line 3,485 ⟶ 3,816:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : RPG Attributes Generator
 
Line 3,530 ⟶ 3,861:
line = line + "])"
see line + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,537 ⟶ 3,868:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">res = []
until res.sum >= 75 && res.count{|n| n >= 15} >= 2 do
res = Array.new(6) do
Line 3,547 ⟶ 3,878:
p res
puts "sum: #{res.sum}"
</syntaxhighlight>
</lang>
{{out}}
<pre>[12, 14, 17, 12, 16, 9]
Line 3,554 ⟶ 3,885:
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">dim statnames$(6)
data "STR", "CON", "DEX", "INT", "WIS", "CHA"
for i = 1 to 6
Line 3,586 ⟶ 3,917:
a = d6() : b = d6() : c = d6() : d = d6()
rollstat = a + b + c + d - min(min(a, b), min(c, d))
end function</langsyntaxhighlight>
 
=={{header|Rust}}==
Line 3,592 ⟶ 3,923:
{{libheader|rand}}
{{works with|Rust|2018}}
<langsyntaxhighlight lang="rust">
use rand::distributions::Uniform;
use rand::prelude::{thread_rng, ThreadRng};
Line 3,669 ⟶ 4,000:
}
}
</syntaxhighlight>
</lang>
{{out}}
Sample output, running the generator ten times:
Line 3,687 ⟶ 4,018:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">
import scala.util.Random
Random.setSeed(1)
Line 3,708 ⟶ 4,039:
 
println("picked => " + getCharacter.mkString("[", ",", "]"))
</syntaxhighlight>
</lang>
 
<pre>
Line 3,719 ⟶ 4,050:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const proc: main is func
Line 3,756 ⟶ 4,087:
writeln(" ----");
writeln("Total " <& total lpad 3);
end func;</langsyntaxhighlight>
 
{{out}}
Line 3,774 ⟶ 4,105:
{{trans|FreeBASIC}}
{{works with|QBasic}}
<langsyntaxhighlight lang="qbasic">FUNCTION min(a, b)
IF a < b THEN LET min = a ELSE LET min = b
END FUNCTION
Line 3,815 ⟶ 4,146:
PRINT "--------"
PRINT "TOT: "; sum
END</langsyntaxhighlight>
{{out}}
<pre>Igual que la entrada de FreeBASIC.</pre>
 
=={{header|uBasic/4tH}}==
{{trans|Yabasic}}
<syntaxhighlight lang="text">dim @n(6)
@n(0) := "STR"
@n(1) := "CON"
@n(2) := "DEX"
@n(3) := "INT"
@n(4) := "WIS"
@n(5) := "CHA"
dim @s(6)
do
s = 0 : n = 0
for i = 0 to 5
@s(i) = FUNC(_rollstat)
s = s + @s(i)
if @s(i) > 14 then n = n + 1
next
until (s > 74) * (n > 1)
loop
for i = 0 to 5
print show(@n(i)); ":"; using "__", @s(i)
next
 
print "----" : Print "TOT: "; using "__", s
end
' simulates a marked regular hexahedron coming to rest on a plane
_d6 return (1 + rnd(6))
 
' rolls four dice, returns the sum of the three highest
_rollstat
local (4)
a@ = FUNC(_d6) : b@ = FUNC(_d6) : c@ = FUNC(_d6) : d@ = FUNC(_d6)
return (a@ + b@ + c@ + d@ - Min(Min(a@, b@), Min(c@, d@)))</syntaxhighlight>
{{Out}}
<pre>STR: 17
CON: 15
DEX: 8
INT: 12
WIS: 14
CHA: 16
----
TOT: 82
 
0 OK, 0:384</pre>
=={{header|UNIX Shell}}==
{{works with|Bourne Again SHell}}
Line 3,824 ⟶ 4,202:
{{works with|Zsh}}
 
<langsyntaxhighlight lang="sh">function main {
typeset attrs=(str dex con int wis cha)
typeset -A values
Line 3,860 ⟶ 4,238:
 
main "$@"
</syntaxhighlight>
</lang>
{{Out}}
<pre>str: 12
Line 3,871 ⟶ 4,249:
=={{header|Visual Basic .NET}}==
repeats until a successful outcome occurs
<langsyntaxhighlight lang="vbnet">Module Module1
 
Dim r As New Random
Line 3,896 ⟶ 4,274:
Loop Until good
End Sub
End Module</langsyntaxhighlight>
{{out}}
sample outputs:
Line 3,913 ⟶ 4,291:
=={{header|Wren}}==
{{libheader|Wren-sort}}
<langsyntaxhighlight ecmascriptlang="wren">import "random" for Random
import "./sort" for Sort
 
var rand = Random.new()
Line 3,935 ⟶ 4,313:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 3,942 ⟶ 4,320:
The six values are: [15, 16, 10, 12, 13, 13]
Their total is: 79
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">func Gen; \Return sum of the three largest of four random values
int I, R, Min, SI, Sum, Die(4);
[Min:= 7; Sum:= 0;
for I:= 0 to 4-1 do
[R:= Ran(6)+1; \R = 1..6
if R < Min then
[Min:= R; SI:= I];
Sum:= Sum+R;
Die(I):= R;
];
return Sum - Die(SI);
];
 
int Total, Count, J, Value(6);
[repeat Total:= 0; Count:= 0;
for J:= 0 to 6-1 do
[Value(J):= Gen;
if Value(J) >= 15 then Count:= Count+1;
Total:= Total + Value(J);
];
until Total >= 75 and Count >= 2;
Text(0, "Total: "); IntOut(0, Total); CrLf(0);
for J:= 0 to 6-1 do
[IntOut(0, Value(J)); ChOut(0, ^ )];
CrLf(0);
]</syntaxhighlight>
 
{{out}}
<pre>
Total: 79
13 17 11 10 16 12
</pre>
 
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="freebasic">sub d6()
//simulates a marked regular hexahedron coming to rest on a plane
return 1 + int(ran(6))
Line 3,983 ⟶ 4,395:
next i
print "-------\nTOT: ", sum
end</langsyntaxhighlight>
{{out}}
<pre>Igual que la entrada de FreeBASIC.</pre>
 
=={{header|Zig}}==
<syntaxhighlight lang="zig">const std = @import("std");
 
const dice = 6;
const rolls = 4;
const stat_count = 6;
 
// requirements
const min_stat_sum = 75;
const min_high_stat_count = 2;
const high_stat_threshold = 15;
 
const RollResult = struct { stats: [stat_count]u16, total: u16 };
 
fn roll_attribute(rand: std.rand.Random) u16 {
var min_roll: u16 = dice;
var total: u16 = 0;
 
for (0..rolls) |_| {
const roll = rand.uintAtMost(u16, dice - 1) + 1;
if (min_roll > roll) {
min_roll = roll;
}
total += roll;
}
 
return total - min_roll;
}
 
fn roll_stats(rand: std.rand.Random) RollResult {
var result: RollResult = RollResult{
.stats = undefined,
.total = 0,
};
var high_stat_count: u16 = 0;
 
var i: u16 = 0;
while (i < stat_count) {
// roll a stat
result.stats[i] = roll_attribute(rand);
result.total += result.stats[i];
if (result.stats[i] >= high_stat_threshold) high_stat_count += 1;
 
// find the maximum possible total
const stats_remain = stat_count - i - 1;
const max_possible_total = result.total + dice * (rolls - 1) * stats_remain;
 
// if it is below the minimum or there are not enough stats over 15 reset
if (max_possible_total < min_stat_sum or high_stat_count + stats_remain < 2) {
i = 0;
result.total = 0;
high_stat_count = 0;
} else {
i += 1;
}
}
 
return result;
}
 
pub fn main() !void {
// Create random generator
var prng = std.rand.DefaultPrng.init(blk: {
var seed: u64 = undefined;
try std.os.getrandom(std.mem.asBytes(&seed));
break :blk seed;
});
const rand = prng.random();
 
const stats = roll_stats(rand);
const stdout = std.io.getStdOut().writer();
 
try stdout.print("Total: {}\n", .{stats.total});
try stdout.print("Stats: [ ", .{});
for (stats.stats) |stat| {
try stdout.print("{} ", .{stat});
}
try stdout.print("]\n", .{});
}
</syntaxhighlight>
{{out}}
<pre>Total: 79
Stats: [ 14 13 5 16 15 16 ]</pre>
 
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">reg attrs=List(), S,N;
do{
attrs.clear();
Line 3,997 ⟶ 4,494:
}while((S=attrs.sum(0))<75 or (N=attrs.filter('>=(15)).len())<2);
println("Random numbers: %s\nSums to %d, with %d >= 15"
.fmt(attrs.concat(","),S,N));</langsyntaxhighlight>
{{out}}
<pre>
1,983

edits