RPG attributes generator: Difference between revisions

Added Easylang
mNo edit summary
(Added Easylang)
 
(84 intermediate revisions by 39 users not shown)
Line 1:
{{task}}
[[Category:Simple]]
'''RPG''' &nbsp; = &nbsp; <u>R</u>ole <u>P</u>laying <u>G</u>ame.
 
 
 
You're running a tabletop RPG, and your players are creating characters.
 
Line 16 ⟶ 20:
However, this can require a lot of manual dice rolling. A programatic solution would be much faster.
 
'''Task'''
 
;Task:
Write a program that:
# Generates 4 random, whole values between 1 and 6.
Line 28 ⟶ 32:
* At least 2 of the values must be 15 or more.
<p></p>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">random:seed(Int(Time().unix_time()))
V total = 0
V count = 0
 
[Int] attributes
L total < 75 | count < 2
attributes = (0..5).map(attribute -> (sum(sorted((0..3).map(roll -> random:(1 .. 6)))[1..])))
 
L(attribute) attributes
I attribute >= 15
count++
 
total = sum(attributes)
 
print(total‘ ’attributes)</syntaxhighlight>
 
{{out}}
<pre>
76 [13, 13, 11, 14, 8, 17]
</pre>
 
=={{header|8086 Assembly}}==
 
<syntaxhighlight lang="asm"> bits 16
cpu 8086
putch: equ 2h
time: equ 2ch
org 100h
section .text
mov ah,time ; Retrieve system time from MS-DOS
int 21h
call rseed ; Seed the RNG
rolls: xor ah,ah ; AH=0 (running total)
mov dx,6 ; DH=0 (amount >=15), DL=6 (counter)
mov di,attrs
attr: call roll4 ; Roll an attribute
mov al,14
cmp al,bh ; Set carry if BH=15 or more
mov al,bh ; AL = roll
sbb bh,bh ; BH=0 if no carry, -1 if carry
sub dh,bh ; DH+=1 if >=15
add ah,al ; Add to running total
mov [di],al ; Save roll
inc di ; Next memory address
dec dl ; One fewer roll left
jnz attr
cmp ah,75 ; Rolling total < 75?
jb rolls ; Then roll again.
cmp dh,2 ; Fewer than 2 attrs < 15?
jb rolls ; Then roll again.
;;; Print the attributes
mov cx,6 ; 6 attributes
p2: mov bl,10 ; divide by 10 to get digits
mov di,attrs
print: xor ah,ah ; AX = attribute
mov al,[di]
div bl ; divide by 10
add ax,3030h ; add '0' to quotient (AH) and remainder (AL)
mov dx,ax
mov ah,putch
int 21h ; print quotient first
mov dl,dh ; then remainder
int 21h
mov dl,' ' ; then a space
int 21h
inc di ; Next attribute
loop print
ret ; Back to DOS
;;; Do 4 rolls, and get result of max 3
roll4: xor bx,bx ; BH = running total
dec bl ; BL = lowest value
mov cx,4 ; Four times
.roll: call d6 ; Roll D6
cmp al,bl ; Lower than current lowest value?
jnb .high ; If so,
mov bl,al ; Set new lowest value
.high: add bh,al ; Add to running total
loop .roll ; If not 4 rolls yet, loop back
sub bh,bl ; Subtract lowest value (giving sum of high 3)
ret
;;; Roll a D6
d6: call rand ; Get random number
and al,7 ; Between 0 and 7
cmp al,6 ; If 6 or higher,
jae d6 ; Then get new random number
inc al ; [1..6] instead of [0..5]
ret
;;; Seed the random number generator with 4 bytes
rseed: xor [rnddat],cx
xor [rnddat+2],dx
;;; "X ABC" random number generator
;;; Generates 8-bit random number in AL
rand: push cx ; Keep registers
push dx
mov cx,[rnddat] ; CL=X CH=A
mov dx,[rnddat+2] ; DL=B DH=C
xor ch,dh ; A ^= C
xor ch,cl ; A ^= X
add dl,ch ; B += A
mov al,dl ; R = B
shr al,1 ; R >>= 1
xor al,ch ; R ^= A
add al,dh ; R += C
mov dh,al ; C = R
mov [rnddat],cx ; Store new state
mov [rnddat+2],dx
pop dx ; Restore registers
pop cx
ret
section .bss
rnddat: resb 4 ; RNG state
attrs: resb 6 ; Rolled attributes</syntaxhighlight>
 
{{out}}
 
<pre>C:\>roll
13 17 09 17 13 14
C:\>roll
17 15 12 14 18 13
C:\>roll
16 12 17 10 11 13
C:\>roll
11 10 16 14 17 14 </pre>
 
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program rpg64.s */
/*******************************************/
/* Constantes file */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
 
.equ NBTIRAGES, 4
.equ NBTIRAGESOK, 3
.equ NBVALUES, 6
.equ TOTALMIN, 75
.equ MAXVALUE, 15
.equ NBMAXVALUE, 2
/*********************************/
/* Initialized data */
/*********************************/
.data
sMessResult: .asciz "Value = @ \n"
szCarriageReturn: .asciz "\n"
sMessResultT: .asciz "Total = @ \n"
sMessResultQ: .asciz "Values above 15 = @ \n"
 
.align 4
qGraine: .quad 123456789
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
tqTirages: .skip 8 * NBTIRAGES
tqValues: .skip 8 * NBVALUES
 
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
1: // begin loop 1
mov x2,0 // counter value >15
mov x4,0 // loop indice
mov x5,0 // total
ldr x3,qAdrtqValues // table values address
2:
bl genValue // call generate value
str x0,[x3,x4,lsl 3] // store in table
add x5,x5,x0 // compute total
cmp x0,MAXVALUE // count value >= 15
add x6,x2,1
csel x2,x6,x2,ge
add x4,x4,1 // increment indice
cmp x4,NBVALUES // end ?
blt 2b
cmp x5,TOTALMIN // compare 75
blt 1b // < loop
cmp x2,#NBMAXVALUE // compare value > 15
blt 1b // < loop
ldr x0,qAdrtqValues // display values
bl displayTable
mov x0,x5 // total
 
ldr x1,qAdrsZoneConv // display value
bl conversion10 // call conversion decimal
ldr x0,qAdrsMessResultT
ldr x1,qAdrsZoneConv
bl strInsertAtCharInc // insert result at @ character
bl affichageMess // display message
mov x0,x2 // counter value > 15
ldr x1,qAdrsZoneConv // display value
bl conversion10 // call conversion decimal
ldr x0,qAdrsMessResultQ
ldr x1,qAdrsZoneConv
bl strInsertAtCharInc // insert result at @ character
bl affichageMess // display message
100: // standard end of the program
mov x0,0 // return code
mov x8,EXIT // request to exit program
svc 0 // perform the system call
qAdrsZoneConv: .quad sZoneConv
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrsMessResult: .quad sMessResult
qAdrsMessResultT: .quad sMessResultT
qAdrsMessResultQ: .quad sMessResultQ
qAdrtqValues: .quad tqValues
/******************************************************************/
/* generate value */
/******************************************************************/
/* x0 returns the value */
genValue:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
mov x3,0 // indice loop
ldr x1,qAdrtqTirages // table tirage address
1:
mov x0,6
bl genereraleas // result 0 to 5
add x0,x0,#1 // for 1 to 6
str x0,[x1,x3,lsl 3] // store tirage
add x3,x3,1 // increment indice
cmp x3,NBTIRAGES // end ?
blt 1b // no -> loop
ldr x0,qAdrtqTirages // table tirage address
mov x1,#0 // first item
mov x2,#NBTIRAGES // number of tirages
bl shellSort // sort table decreasing
mov x3,#0 // raz indice loop
mov x0,#0 // total
ldr x1,qAdrtqTirages // table tirage address
2:
ldr x2,[x1,x3,lsl 3] // read tirage
add x0,x0,x2 // compute sum
add x3,x3,1 // increment indice
cmp x3,NBTIRAGESOK // end ?
blt 2b
100:
ldp x2,x3,[sp],16 // restaur 2 registers
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
qAdrtqTirages: .quad tqTirages
/***************************************************/
/* shell Sort decreasing */
/***************************************************/
/* x0 contains the address of table */
/* x1 contains the first element but not use !! */
/* this routine use first element at index zero !!! */
/* x2 contains the number of element */
shellSort:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
stp x4,x5,[sp,-16]! // save registers
stp x6,x7,[sp,-16]! // save registers
stp x8,x9,[sp,-16]! // save registers
sub x2,x2,1 // index last item
mov x1,x2 // init gap = last item
1: // start loop 1
lsr x1,x1,1 // gap = gap / 2
cbz x1,100f // if gap = 0 -> end
mov x3,x1 // init loop indice 1
2: // start loop 2
ldr x4,[x0,x3,lsl 3] // load first value
mov x5,x3 // init loop indice 2
3: // start loop 3
cmp x5,x1 // indice < gap
blt 4f // yes -> end loop 2
sub x6,x5,x1 // index = indice - gap
ldr x8,[x0,x6,lsl 3] // load second value
cmp x4,x8 // compare values
ble 4f
str x8,[x0,x5,lsl 3] // store if >
sub x5,x5,x1 // indice = indice - gap
b 3b // and loop
4: // end loop 3
str x4,[x0,x5,lsl 3] // store value 1 at indice 2
add x3,x3,1 // increment indice 1
cmp x3,x2 // end ?
ble 2b // no -> loop 2
b 1b // yes loop for new gap
100: // end function
ldp x8,x9,[sp],16 // restaur 2 registers
ldp x6,x7,[sp],16 // restaur 2 registers
ldp x4,x5,[sp],16 // restaur 2 registers
ldp x2,x3,[sp],16 // restaur 2 registers
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/******************************************************************/
/* Display table elements */
/******************************************************************/
/* x0 contains the address of table */
displayTable:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
mov x2,x0 // table address
mov x3,0
1: // loop display table
ldr x0,[x2,x3,lsl 3]
ldr x1,qAdrsZoneConv // display value
bl conversion10 // call function
ldr x0,qAdrsMessResult
ldr x1,qAdrsZoneConv
bl strInsertAtCharInc // insert result at @ character
bl affichageMess // display message
add x3,x3,1
cmp x3,NBVALUES - 1
ble 1b
ldr x0,qAdrszCarriageReturn
bl affichageMess
100:
ldp x2,x3,[sp],16 // restaur 2 registers
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
 
/***************************************************/
/* Generation random number */
/* algo xorshift (see wikipedia) */
/***************************************************/
/* x0 contains limit */
genereraleas:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
cbz x0,100f
mov x3,x0 // maxi value
ldr x0,qAdrqGraine // graine
ldr x2,[x0]
lsl x1,x2,13
eor x2,x2,x1
lsr x1,x2,7
eor x2,x2,x1
lsl x1,x2,17
eor x1,x2,x1
str x1,[x0] // sauver graine
udiv x2,x1,x3 //
msub x0,x2,x3,x1 // compute result modulo limit
 
100: // end function
ldp x2,x3,[sp],16 // restaur 2 registers
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/*****************************************************/
qAdrqGraine: .quad qGraine
/********************************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
{{Output}}
<pre>
Value = 11
Value = 13
Value = 16
Value = 11
Value = 14
Value = 18
 
Total = 83
Values above 15 = 2
</pre>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">TYPE Result=[BYTE success,sum,highCount]
BYTE FUNC GenerateAttrib()
BYTE i,v,min,sum
 
min=255 sum=0
FOR i=0 TO 3
DO
v=Rand(6)+1
IF v<min THEN
min=v
FI
sum==+v
OD
RETURN (sum-min)
 
PROC Generate(BYTE ARRAY a BYTE len Result POINTER res)
BYTE i,v,count
res.highCount=0 res.sum=0
FOR i=0 TO len-1
DO
v=GenerateAttrib()
IF v>=15 THEN
res.highCount==+1
FI
res.sum==+v
a(i)=v
OD
IF res.highCount<2 OR res.sum<75 THEN
res.success=0
ELSE
res.success=1
FI
RETURN
 
PROC Main()
DEFINE count="6"
BYTE ARRAY a(count)
Result res
BYTE i
 
res.success=0
WHILE res.success=0
DO
Generate(a,count,res)
Print("attribs: ")
FOR i=0 TO count-1
DO
PrintB(a(i))
IF i<count-1 THEN
Put(',)
FI
OD
PrintF(" sum=%B highCount=%B ",res.sum,res.highCount)
IF res.success THEN
PrintE("success")
ELSE
PrintE("failed")
FI
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/RPG_attributes_generator.png Screenshot from Atari 8-bit computer]
<pre>
attribs: 13,12,13,13,14,16 sum=81 highCount=1 failed
attribs: 10,10,15,9,16,8 sum=68 highCount=2 failed
attribs: 14,10,7,9,12,12 sum=64 highCount=0 failed
attribs: 12,13,16,16,18,16 sum=91 highCount=4 success
</pre>
 
=={{header|ALGOL 68}}==
{{Trans|Action!}}
<syntaxhighlight lang="algol68">BEGIN # RPG attributes generator #
 
INT attrib count = 6;
MODE RESULT = STRUCT( BOOL success, INT sum, high count, [ 1 : attrib count ]INT a );
 
PROC generate attrib = INT:
BEGIN
INT min := 255, sum := 0;
FOR i FROM 0 TO 3 DO
INT v = ENTIER( next random * 6 ) + 1;
IF v < min THEN
min := v
FI;
sum +:= v
OD;
sum - min
END # generate attrib #;
 
PROC generate = ( REF RESULT res )VOID:
BEGIN
high count OF res := 0;
sum OF res := 0;
FOR i FROM LWB a OF res TO UPB a OF res DO
INT v = generate attrib;
IF v >= 15 THEN
high count OF res +:= 1
FI;
sum OF res +:= v;
( a OF res )[ i ] := v
OD;
success OF res := ( high count OF res >= 2 AND sum OF res >= 75 )
END # generate # ;
 
RESULT res;
success OF res := FALSE;
WHILE NOT success OF res DO
generate( res );
print( ( "attribs: " ) );
FOR i FROM LWB a OF res TO UPB a OF res DO
print( ( whole( ( a OF res )[ i ], 0 ) ) );
IF i < UPB a OF res THEN
print( ( " " ) )
FI
OD;
print( ( " sum=", whole( sum OF res, 0 )
, " highCount=", whole( high count OF res, 0 )
, " ", IF success OF res THEN "success" ELSE "failed" FI
, newline
)
)
OD
 
END</syntaxhighlight>
{{out}}
<pre>
attribs: 12 7 15 12 7 13 sum=66 highCount=1 failed
attribs: 10 10 11 13 9 9 sum=62 highCount=0 failed
attribs: 10 13 10 15 10 11 sum=69 highCount=1 failed
attribs: 10 16 15 11 16 8 sum=76 highCount=3 success
</pre>
 
=={{header|APL}}==
{{works with|Dyalog APL}}
 
<syntaxhighlight lang="apl">roll←{(+/-⌊/)¨?¨6/⊂4/6}⍣{(75≤+/⍺)∧2≤+/⍺≥15}</syntaxhighlight>
 
{{out}}
 
<pre> roll''
10 9 13 16 11 17
roll''
18 14 12 8 15 14
roll''
16 16 15 17 16 11
roll''
15 14 8 15 12 15</pre>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
 
/* ARM assembly Raspberry PI */
Line 354 ⟶ 888:
bx lr
 
</syntaxhighlight>
</lang>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">vals: []
 
while [or? 75 > sum vals
2 > size select vals => [&>=15]] [
vals: new []
 
while [6 > size vals][
rands: new map 1..4 => [random 1 6]
remove 'rands .once (min rands)
'vals ++ sum rands
]
]
 
print ["values:" vals ]
print ["with sum:" sum vals]</syntaxhighlight>
 
=={{header|Atari BASIC}}==
{{trans|Commodore BASIC}}
<syntaxhighlight lang="basic">100 REM RPG character generator
110 DIM AT(5)
120 DIM AT$(18)
130 AT$="StrDexConIntWisCha"
140 PT=0:SA=0
150 PRINT CHR$(125);CHR$(29);CHR$(31);"Rolling..."
160 FOR AI=0 TO 5
170 DT=0:MI=6:REM dice total, min die
180 FOR I=0 TO 3
190 D=INT(RND(1)*6)+1
200 DT=DT+D
210 IF D<MI THEN MI=D
220 NEXT I
230 DT=DT-MI
240 AT(AI)=DT
250 PT=PT+DT
260 IF DT>=15 THEN SA=SA+1
270 NEXT AI
280 IF PT<75 OR SA<2 THEN 140
290 PRINT CHR$(125);"Character Attributes:"
300 PRINT
310 FOR AI=0 TO 5
315 POSITION 10,AI+2
320 PRINT AT$(AI*3+1,AI*3+3);":";
330 POSITION 16-INT(AT(AI)/10),AI+2
340 PRINT AT(AI)
350 NEXT AI
360 POSITION 8,9
370 PRINT "Total: ";PT
380 PRINT
390 PRINT "Do you accept? ";
400 OPEN #1,4,0,"K:"
410 GET #1,K
420 IF K<>78 AND K<>89 THEN 410
430 PRINT CHR$(K)
440 CLOSE #1
450 IF K=78 THEN 140
460 POSITION 0,13
470 PRINT "Excellent. Good luck on your adventure!"</syntaxhighlight>
 
{{Out}}
<pre> Character Attributes:
 
Str: 17
Dex: 15
Con: 13
Int: 11
Wis: 11
Cha: 14
 
Total: 81
 
Do you accept? Y
 
Excellent. Good luck on your adventure!
 
Ready
</pre>
 
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="freebasic">function min(a, b)
if a < b then return a else return b
end function
 
function d6()
#simulates a marked regular hexahedron coming to rest on a plane
return 1 + int(rand * 6)
end function
 
function roll_stat()
#rolls four dice, returns the sum of the three highest
a = d6() : b = d6() : c = d6() : d = d6()
return a + b + c + d - min(min(a, b), min(c, d))
end function
 
arraybase 1
dim statnames$ = {"STR", "CON", "DEX", "INT", "WIS", "CHA"}
dim stat(6)
acceptable = false
 
do
sum = 0 : n15 = 0
for i = 1 to 6
stat[i] = roll_stat()
sum += stat[i]
if stat[i] >= 15 then n15 += 1
next i
if sum >= 75 and n15 >= 2 then acceptable = true
until acceptable
 
for i = 1 to 6
print statnames$[i]; ": "; stat[i]
next i
print "-------"
print "TOT: "; sum
end</syntaxhighlight>
{{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 399 ⟶ 1,061:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 409 ⟶ 1,071:
Their sum is 77 and 3 of them are >= 15
</pre>
 
=={{header|C sharp|C#}}==
{{trans|Visual Basic .NET}}
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
 
static class Module1
{
static Random r = new Random();
 
static List<int> getThree(int n)
{
List<int> g3 = new List<int>();
for (int i = 0; i < 4; i++) g3.Add(r.Next(n) + 1);
g3.Sort(); g3.RemoveAt(0); return g3;
}
 
static List<int> getSix()
{
List<int> g6 = new List<int>();
for (int i = 0; i < 6; i++) g6.Add(getThree(6).Sum());
return g6;
}
 
static void Main(string[] args)
{
bool good = false; do {
List<int> gs = getSix(); int gss = gs.Sum(); int hvc = gs.FindAll(x => x > 14).Count;
Console.Write("attribs: {0}, sum={1}, ({2} sum, high vals={3})",
string.Join(", ", gs), gss, gss >= 75 ? "good" : "low", hvc);
Console.WriteLine(" - {0}", (good = gs.Sum() >= 75 && hvc > 1) ? "success" : "failure");
} while (!good);
}
}</syntaxhighlight>
{{out}}
sample outputs:
<pre>attribs: 10, 11, 11, 11, 11, 14, sum=68, (low sum, high vals=0) - failure
attribs: 16, 13, 12, 10, 15, 16, sum=82, (good sum, high vals=3) - success</pre>
<pre>attribs: 16, 8, 9, 15, 16, 12, sum=76, (good sum, high vals=3) - success</pre>
 
=={{header|C++}}==
GCC 4.9.2, unoptimised.
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <ctime>
#include <iostream>
Line 463 ⟶ 1,165:
return 0;
}</langsyntaxhighlight>
{{out}}
Sample run:
Line 470 ⟶ 1,172:
Total: 83, Values above 15 : 2
</pre>
 
=={{header|C sharp|C#}}==
{{trans|Visual Basic .NET}}
<lang csharp>using System;
using System.Collections.Generic;
using System.Linq;
 
static class Module1
{
static Random r = new Random();
 
static List<int> getThree(int n)
{
List<int> g3 = new List<int>();
for (int i = 0; i < 4; i++) g3.Add(r.Next(n) + 1);
g3.Sort(); g3.RemoveAt(0); return g3;
}
 
static List<int> getSix()
{
List<int> g6 = new List<int>();
for (int i = 0; i < 6; i++) g6.Add(getThree(6).Sum());
return g6;
}
 
static void Main(string[] args)
{
bool good = false; do {
List<int> gs = getSix(); int gss = gs.Sum(); int hvc = gs.FindAll(x => x > 14).Count;
Console.Write("attribs: {0}, sum={1}, ({2} sum, high vals={3})",
string.Join(", ", gs), gss, gss >= 75 ? "good" : "low", hvc);
Console.WriteLine(" - {0}", (good = gs.Sum() >= 75 && hvc > 1) ? "success" : "failure");
} while (!good);
}
}</lang>
{{out}}
sample outputs:
<pre>attribs: 10, 11, 11, 11, 11, 14, sum=68, (low sum, high vals=0) - failure
attribs: 16, 13, 12, 10, 15, 16, sum=82, (good sum, high vals=3) - success</pre>
<pre>attribs: 16, 8, 9, 15, 16, 12, sum=76, (good sum, high vals=3) - success</pre>
 
=={{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 557 ⟶ 1,219:
} while (result '= 3)
quit</langsyntaxhighlight>
{{out}}<pre>
Line 570 ⟶ 1,232:
Total 80 and 2 attributes >=15 are sufficient.
</pre>
 
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">% This program needs to be merged with PCLU's "misc" library
% to use the random number generator.
%
% pclu -merge $CLUHOME/lib/misc.lib -compile rpg_gen.clu
 
% Seed the random number generator with the current time
init_rng = proc ()
d: date := now()
seed: int := ((d.hour*60) + d.minute)*60 + d.second
random$seed(seed)
end init_rng
 
character = cluster is generate
rep = null
% Roll a die
roll_die = proc () returns (int)
return (1 + random$next(6))
end roll_die
 
% Roll four dice and get the sum of the highest three rolls
roll_four_times = proc () returns (int)
lowest: int := 7 % higher than any dice roll
sum: int := 0
for i: int in int$from_to(1,4) do
roll: int := roll_die()
sum := sum + roll
if roll < lowest then lowest := roll end
end
return (sum - lowest)
end roll_four_times
% Try to generate a character by rolling six values
try_generate = proc () returns (sequence[int])
rolls: sequence[int] := sequence[int]$[]
for i: int in int$from_to(1,6) do
rolls := sequence[int]$addh(rolls, roll_four_times())
end
return (rolls)
end try_generate
% See if a character is valid
valid = proc (c: sequence[int]) returns (bool)
total: int := 0
at_least_15: int := 0
for i: int in sequence[int]$elements(c) do
total := total + i
if i >= 15 then at_least_15 := at_least_15 + 1 end
end
return (total >= 75 & at_least_15 >= 2)
end valid
% Generate a character
generate = proc () returns (sequence[int])
while true do
c: sequence[int] := try_generate()
if valid(c) then return(c) end
end
end generate
end character
 
% Generate a character and display the six values
start_up = proc ()
po: stream := stream$primary_output()
init_rng()
hero: sequence[int] := character$generate()
for stat: int in sequence[int]$elements(hero) do
stream$putright(po, int$unparse(stat), 4)
end
end start_up</syntaxhighlight>
{{out}}
<pre>$ ./rpg_gen
14 9 12 15 13 16
$ ./rpg_gen
11 15 14 17 13 13
$ ./rpg_gen
10 16 10 12 16 16
$ ./rpg_gen
16 11 16 11 14 13
$ ./rpg_gen
16 7 14 15 13 10</pre>
 
=={{header|Commodore BASIC}}==
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.)
 
<syntaxhighlight lang="gwbasic">100 rem rpg character roller
110 rem rosetta code - commodore basic
120 dim di(3):rem dice
130 dim at(5),at$(5):rem attributes as follows:
140 at$(0)="Strength"
150 at$(1)="Dexterity"
160 at$(2)="Constitution"
170 at$(3)="Intelligence"
180 at$(4)="Wisdom"
190 at$(5)="Charisma"
200 pt=0:sa=0:rem points total and number of strong attributes (15+)
210 print chr$(147);chr$(14);chr$(29);chr$(17);"Rolling..."
220 for ai=0 to 5:rem attribute index
230 for i=0 to 3:di(i)=int(rnd(.)*6)+1:next i
240 gosub 450
250 dt=0:rem dice total
260 for i=0 to 2:dt=dt+di(i):next i:rem take top 3
270 at(ai)=dt:pt=pt+dt
280 if dt>=15 then sa=sa+1
290 next ai
300 if pt<75 or sa<2 then goto 200
310 print chr$(147);"Character Attributes:"
320 print
330 for ai=0 to 5
340 print spc(13-len(at$(ai)));at$(ai);":";tab(14);at(ai)
350 next ai
360 print
370 print " Total:";tab(14);pt
380 print
390 print "Do you accept? ";
400 get k$:if k$<>"y" and k$<>"n" then 400
410 print k$
420 if k$="n" then goto 200
430 print:print "Excellent. Good luck on your adventure!"
440 end
450 rem "sort" dice - really just put smallest one last
460 for x=0 to 2
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</syntaxhighlight>
 
{{out}}
<nowiki>
ready.
run
 
Rolling...
 
Character Attributes:
 
Strength: 8
Dexterity: 11
Constitution: 16
Intelligence: 14
Wisdom: 12
Charisma: 15
 
Total: 76
 
Do you accept? n
 
Rolling...
 
Character Attributes:
 
Strength: 16
Dexterity: 15
Constitution: 8
Intelligence: 15
Wisdom: 11
Charisma: 15
 
Total: 80
 
Do you accept? y
 
Excellent. Good luck on your adventure!
 
ready.
&#9608;
</nowiki>
 
=={{header|Common Lisp}}==
===Mapping functions===
<syntaxhighlight lang="lisp">
(defpackage :rpg-generator
(:use :cl)
(:export :generate))
(in-package :rpg-generator)
(defun sufficient-scores-p (scores)
(and (>= (apply #'+ scores) 75)
(>= (count-if #'(lambda (n) (>= n 15)) scores) 2)))
 
(defun gen-score ()
(apply #'+ (rest (sort (loop repeat 4 collect (1+ (random 6))) #'<))))
 
(defun generate ()
(loop for scores = (loop repeat 6 collect (gen-score))
until (sufficient-scores-p scores)
finally (format t "Scores: ~A~%" scores)
(format t "Total: ~A~%" (apply #'+ scores))
(format t ">= 15: ~A~%" (count-if (lambda (n) (>= n 15)) scores))
(return scores)))
</syntaxhighlight>
=== 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}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";
include "argv.coh";
 
# There is no random number generator in the standard library (yet?)
# There is also no canonical source of randomness, and indeed,
# on some target platforms (like CP/M) there is no guaranteed
# source of randomness at all. Therefore, I implement the "X ABC"
# random number generator, and ask for a seed on the command line.
 
record RandState is
x @at(0): uint8;
a @at(1): uint8;
b @at(2): uint8;
c @at(3): uint8;
end record;
 
sub RandByte(s: [RandState]): (r: uint8) is
s.x := s.x + 1;
s.a := s.a ^ s.c ^ s.x;
s.b := s.b + s.a;
s.c := s.c + (s.b >> 1) ^ s.a;
r := s.c;
end sub;
 
# Roll a d6
typedef D6 is int(1, 6);
sub Roll(s: [RandState]): (r: D6) is
var x: uint8;
loop
x := RandByte(s) & 7;
if x < 6 then break; end if;
end loop;
r := x + 1;
end sub;
 
# Roll 4 D6es and get the sum of the 3 highest
sub Roll4(s: [RandState]): (r: uint8) is
r := 0;
var lowest: uint8 := 0;
var n: uint8 := 4;
while n > 0 loop
var roll := Roll(s);
r := r + roll;
if lowest > roll then
lowest := roll;
end if;
n := n - 1;
end loop;
r := r - lowest;
end sub;
 
# Read random seed from command line
var randState: RandState;
 
ArgvInit();
var argmt := ArgvNext();
if argmt == (0 as [uint8]) then
print("Please give random seed on command line.\n");
ExitWithError();
end if;
([&randState as [int32]], argmt) := AToI(argmt);
 
var total: uint8;
var attrs: uint8[6];
var i: @indexof attrs;
loop
var at15: uint8 := 0;
i := 0;
total := 0;
# generate 6 attributes
while i < 6 loop
attrs[i] := Roll4(&randState);
total := total + attrs[i];
# count how many are higher than or equal to 15
if attrs[i] >= 15 then
at15 := at15 + 1;
end if;
i := i + 1;
end loop;
# if the requirements are met, then stop
if total >= 75 and at15 >= 2 then
break;
end if;
end loop;
 
# Show the generated values
print("Attributes: ");
i := 0;
while i < 6 loop
print_i8(attrs[i]);
print_char(' ');
i := i + 1;
end loop;
print("\nTotal: ");
print_i8(total);
print_nl();</syntaxhighlight>
 
{{out}}
 
<pre>$ ./rpgroll.386 123
Attributes: 14 15 23 13 18 12
Total: 95
$ ./rpgroll.386 124
Attributes: 10 17 11 12 14 17
Total: 81
$ ./rpgroll.386 125
Attributes: 6 21 13 14 23 19
Total: 96</pre>
 
=={{header|Crystal}}==
<langsyntaxhighlight Rubylang="ruby">def roll_stat
dices = Array(Int32).new(4) { rand(1..6) }
dices.sum - dices.min
Line 587 ⟶ 1,596:
stats = roll_character
puts "stats: #{stats}, sum is #{stats.sum}"
end</langsyntaxhighlight>
 
sample output:
Line 600 ⟶ 1,609:
stats: [17, 13, 11, 10, 14, 16], sum is 81
stats: [11, 16, 11, 13, 15, 16], sum is 82</pre>
 
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{libheader| System.Generics.Collections}}
{{Trans|C#}}
<syntaxhighlight lang="delphi">
program RPG_Attributes_Generator;
 
{$APPTYPE CONSOLE}
 
{$R *.res}
 
uses
System.SysUtils,
System.Generics.Collections;
 
type
TListOfInt = class(TList<Integer>)
public
function Sum: Integer;
function FindAll(func: Tfunc<Integer, Boolean>): TListOfInt;
function Join(const sep: string): string;
end;
 
{ TListOfInt }
 
function TListOfInt.FindAll(func: Tfunc<Integer, Boolean>): TListOfInt;
var
Item: Integer;
begin
Result := TListOfInt.Create;
if Assigned(func) then
for Item in self do
if func(Item) then
Result.Add(Item);
end;
 
function TListOfInt.Join(const sep: string): string;
var
Item: Integer;
begin
Result := '';
for Item in self do
if Result.IsEmpty then
Result := Item.ToString
else
Result := Result + sep + Item.ToString;
end;
 
function TListOfInt.Sum: Integer;
var
Item: Integer;
begin
Result := 0;
for Item in self do
Inc(Result, Item);
end;
 
function GetThree(n: integer): TListOfInt;
var
i: Integer;
begin
Randomize;
Result := TListOfInt.Create;
for i := 0 to 3 do
Result.Add(Random(n) + 1);
Result.Sort;
Result.Delete(0);
end;
 
function GetSix(): TListOfInt;
var
i: Integer;
tmp: TListOfInt;
begin
Result := TListOfInt.Create;
for i := 0 to 5 do
begin
tmp := GetThree(6);
Result.Add(tmp.Sum);
tmp.Free;
end;
end;
 
const
GOOD_STR: array[Boolean] of string = ('low', 'good');
SUCCESS_STR: array[Boolean] of string = ('failure', 'success');
 
var
good: Boolean;
gs, hvcList: TListOfInt;
gss, hvc: Integer;
 
begin
good := false;
repeat
gs := GetSix();
gss := gs.Sum;
 
hvcList := gs.FindAll(
function(x: Integer): Boolean
begin
result := x > 14;
end);
hvc := hvcList.Count;
hvcList.Free;
 
Write(Format('Attribs: %s, sum=%d, (%s sum, high vals=%d)', [gs.Join(', '),
gss, GOOD_STR[gss >= 75], hvc]));
 
good := (gss >= 75) and (hvc > 1);
 
Writeln(' - ', SUCCESS_STR[good]);
 
gs.Free;
until good;
Readln;
end.
 
</syntaxhighlight>
 
{{out}}
<pre>
Attribs: 8, 15, 15, 16, 14, 10, sum=78, (good sum, high vals=3) - success
</pre>
 
=={{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
}
 
func getSix() {
var g6 = []
for i in 0..5 {
g6.addAdd(getThree(6).sumSum())
}
g6
}
 
func Array.sumSum() {
var acc = 0
for x in this {
Line 628 ⟶ 1,762:
acc
}
 
func Array.findAllFindAll(pred) {
for x in this when pred(x) {
yield x
Line 636 ⟶ 1,770:
var good = false
 
while !good {
var gs = getSix()
var gss = gs.sumSum()
var hvc = gs.findAllFindAll(x => x > 14).lenLength()
print("attribs: \(String.join(gs, separator: ", ")), sum=\(gss), (\(if gss >= 75 { "good" } else { "low" }) sum, high vals=\(hvc))", terminator: "")
goodlet gl = gs.sum()gss >= 75 &&? hvc"good" >: 1"low"
print("(\(gl) -sum, " +high vals=\(if good { "successhvc))", } else {terminator: "failure" }))
good = gs.Sum() >= 75 && hvc > 1
}</lang>
print(" - " + (good ? "success" : "failure"))
}</syntaxhighlight>
 
{{out}}
 
<pre>attribs: [103, 133, 110, 130, 120, 101], sum=697, (good sum, high vals=6) - success</pre>
 
 
=={{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 "Attribues: " & " " & 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 701 ⟶ 1,847:
: generate-valid-stats ( -- seq )
fstats [ dup valid-stats? ] [ drop stats ] do until ;
: stats-info ( seq -- )
Line 711 ⟶ 1,857:
[ "%s: %d\n" printf ] 2each nl stats-info ;
MAIN: main</langsyntaxhighlight>
{{out}}
<pre>
Line 724 ⟶ 1,870:
# of attributes >= 15: 2
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">#define min(a, b) iif(a < b, a, b)
 
function d6() as integer
'simulates a marked regular hexahedron coming to rest on a plane
return 1+int(rnd*6)
end function
 
function roll_stat() as integer
'rolls four dice, returns the sum of the three highest
dim as integer a=d6(), b=d6(), c=d6(), d=d6()
return a + b + c + d - min( min(a, b), min(c, d) )
end function
 
dim as string*3 statnames(1 to 6) = {"STR", "CON", "DEX", "INT", "WIS", "CHA"}
dim as integer stat(1 to 6), n15, sum
dim as boolean acceptable = false
 
randomize timer
do
sum = 0
n15 = 0
for i as integer = 1 to 6
stat(i) = roll_stat()
sum = sum + stat(i)
if stat(i)>=15 then n15 += 1
next i
if sum>=75 and n15 >=2 then acceptable = true
loop until acceptable
 
for i as integer = 1 to 6
print using "&: ##";statnames(i);stat(i)
next i
print "--------"
print using "TOT: ##";sum</syntaxhighlight>
{{out}}<pre>STR: 14
CON: 11
DEX: 15
INT: 14
WIS: 13
CHA: 15
--------
TOT: 82
</pre>
 
=={{header|FOCAL}}==
<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.30 I (T-75)1.1
01.40 S K=0;F X=1,6;D 2
01.50 I (K-2)1.1
01.55 T "STR DEX CON INT WIS CHA TOTAL",!
01.60 F X=1,6;T %2,AT(X)," "
01.70 T T,!
01.80 Q
 
02.10 I (AT(X)-15)2.3,2.2,2.2
02.20 S K=K+1
02.30 R
 
04.01 C--ROLL 4 D6ES AND GIVE SUM OF LARGEST 3
04.10 S XS=7;S S3=0;S XN=4
04.20 D 6;S S3=S3+A;I (XS-A)4.4,4.4,4.3
04.30 S XS=A
04.40 S XN=XN-1;I (XN),4.5,4.2
04.50 S S3=S3-XS
 
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)</syntaxhighlight>
 
{{out}}
 
<pre>*G
STR DEX CON INT WIS CHA TOTAL
= 16 = 12 = 14 = 8 = 15 = 12 = 77
*G
STR DEX CON INT WIS CHA TOTAL
= 9 = 13 = 8 = 17 = 13 = 16 = 76
*G
STR DEX CON INT WIS CHA TOTAL
= 12 = 14 = 7 = 15 = 12 = 17 = 77
*G
STR DEX CON INT WIS CHA TOTAL
= 11 = 16 = 16 = 11 = 14 = 10 = 78
*G
STR DEX CON INT WIS CHA TOTAL
= 15 = 15 = 13 = 13 = 11 = 13 = 80
*G
STR DEX CON INT WIS CHA TOTAL
= 16 = 9 = 16 = 9 = 11 = 14 = 75</pre>
 
=={{header|Forth}}==
{{works with|GNU Forth}}
{{libheader|random.fs}}
<syntaxhighlight lang="forth">require random.fs
: d6 ( -- roll ) 6 random 1 + ;
 
variable smallest
: genstat ( -- stat )
d6 dup smallest !
3 0 do
d6 dup smallest @ < if dup smallest ! then +
loop
smallest @ -
;
 
variable strong
variable total
: genstats ( -- cha wis int con dex str )
0 strong !
0 total !
6 0 do
genstat
dup 15 >= if strong @ 1 + strong ! then
dup total @ + total !
loop
total @ 75 < strong @ 2 < or if
drop drop drop drop drop drop
recurse
then
;
 
: roll ( -- )
genstats
." str:" . ." dex:" . ." con:" .
." int:" . ." wis:" . ." cha:" .
." (total:" total @ . 8 emit ." )"
;
 
utime drop seed !</syntaxhighlight>
 
{{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 771 ⟶ 2,118:
break
}
}</langsyntaxhighlight>
 
{{out}}
Line 783 ⟶ 2,130:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Control.Monad (replicateM)
import System.Random (randomRIO)
import Data.Bool (bool)
import Data.List (sort)
 
Line 791 ⟶ 2,139:
discardUntil
(((&&) . (75 <) . sum) <*> ((2 <=) . length . filter (15 <=)))
(replicateM 6 $ sum . tail . sort <$> replicateM 4 (randomRIO (1, 6 :: Int)))
(sum . tail . sort) <$> replicateM 4 (randomRIO (1, 6 :: Int)))
 
discardUntil :: ([Int] -> Bool) -> IO [Int] -> IO [Int]
discardUntil p throw = go
where
let go =
go = throw >>= (<*>) (bool go . return) p
\x ->
if p x
then return x
else go
in go
 
-- TEST -------------------------------- TEST ---------------------------
main :: IO ()
main = replicateM 10 character >>= mapM_ (print . (sum >>= (,)))</langsyntaxhighlight>
{{Out}}
<pre>Sample computation:
Line 823 ⟶ 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 836 ⟶ 2,178:
NB. use: generate_character 'name'
generate_character=: (; (+/ ; ])@:([: generate_attributes@:show Until accept 0:))&>@:boxopen
</syntaxhighlight>
</lang>
 
 
Line 869 ⟶ 2,211:
 
=={{header|Java}}==
<syntaxhighlight lang="java">import java.util.List;
<lang Java>
import java.util.Random;
static boolean goodRoll = false;
import java.util.stream.Stream;
 
public static int genAttribute(){
import static java.util.stream.Collectors.toList;
// Create a new Random object to populate our array with. We use nextInt(6)+1 because 6 is exclusive and 0 is inclusive
 
Random dice = new Random();
public class Rpg {
int[] sumArray = {dice.nextInt(6)+1, dice.nextInt(6)+1, dice.nextInt(6)+1, dice.nextInt(6)+1};
 
private static final Random random = new Random();
// Sort the array ascending, last 3 dice will always be the highest, return sum.
java.util.Arrays.sort(sumArray);
public static int genAttribute() {
return (sumArray[1] + sumArray[2] + sumArray[3]);
return random.ints(1, 6 + 1) // Throw dices between 1 and 6
.limit(4) // Do 5 throws
.sorted() // Sort them
.limit(3) // Take the top 3
.sum(); // Sum them
}
public static boolean checkFinalArray(int[] checkArray){
int fifteenCount = 0;
// First check for how many 15+'s
for (int z : checkArray){
if (z >= 15){
fifteenCount++;
}
}
return (fifteenCount >= 2 && Arrays.stream(checkArray).sum() >= 75);
}
public static void main(String[] args) {
while (true) {
// Here we use a while loop to make sure that while the conditions aren't met, we reroll.
while (!goodRoll){ List<Integer> stats =
Stream.generate(Rpg::genAttribute) // Generate some stats
int[] finalArray;
finalArray = new int[ .limit(6) // Take 6];
.collect(toList()); // Save them in an array
 
//int Generatesum 6= attributes using above method genAttributesstats.stream().mapToInt(Integer::intValue).sum();
forlong count = stats.stream().filter(intv i-> v >= 0; i<6;++i15){.count();
if (count >= 2 finalArray[i]&& sum >= genAttribute(75); {
System.out.printf("The 6 random numbers generated are: %s\n", stats);
}
System.out.printf("Their sum is %s and %s of them are >= 15\n", sum, count);
// Pass finalArray to be checked
if (checkFinalArray(finalArray)){ return;
System.out.println("sum: " + Arrays.stream(finalArray).sum());
// Enhanced for to print each die
for (int x : finalArray){
System.out.println(x);
}
goodRoll = true; // Exit the loop if conditions are met.
}
}
}
}</syntaxhighlight>
</lang>
{{Out}}
<pre>
The 6 random numbers generated are: [10, 18, 9, 15, 14, 14]
sum: 79
Their sum is 80 and 2 of them are >= 15
10
16
14
16
8
15
</pre>
 
=={{header|Javascript}}==
===Imperative===
<langsyntaxhighlight lang="javascript">function roll() {
const stats = {
total: 0,
Line 963 ⟶ 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 976 ⟶ 2,299:
===Functional===
{{Trans|Python}} (Functional composition version)
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 1,099 ⟶ 2,422:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
A sample of 10 character attribute sets:
Line 1,112 ⟶ 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 1,135 ⟶ 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 1,142 ⟶ 2,558:
 
=={{header|Kotlin}}==
<syntaxhighlight lang="kotlin">import kotlin.random.Random
<lang scala>// Version 1.2.51
 
fun main() {
import java.util.Random
 
fun main(args: Array<String>) {
val r = Random()
while (true) {
val values = IntArrayList(6) {
val rolls = generateSequence { 1 + Random.nextInt(6) }.take(4)
for (i in 0..5) {
val numbers = IntArrayrolls.sorted().take(43) { 1 + r.nextIntsum(6) }
numbers.sort()
values[i] = numbers.drop(1).sum()
}
val vsum = values.sum()
val vcount = values.count { it >= 15 }
if (vsum < 75 || vcount < 2) continue
println("The 6 random numbers generated are: $values")
println(values.asList()"Their sum is $vsum and $vcount of them are >= 15")
println("\nTheir sum is $vsum and $vcount of them are >= 15")
break
}
}</langsyntaxhighlight>
 
{{output}}
Sample run:
<pre>
The 6 random numbers generated are: [13, 14, 13, 15, 17, 8]
[13, 14, 13, 15, 17, 8]
 
Their sum is 80 and 2 of them are >= 15
</pre>
 
=={{header|Ksh}}==
<syntaxhighlight lang="ksh">
#!/bin/ksh
 
# RPG attributes generator
 
# # Variables:
#
typeset -a attribs=( strength dexterity constitution intelligence wisdom charisma )
integer MINTOT=75 MIN15S=2
 
# # Functions:
#
# # Function _diceroll(sides, number, reportAs) - roll number of side-sided
# # dice, report (s)sum or (a)array (pseudo) of results
#
function _diceroll {
typeset _sides ; integer _sides=$1 # Number of sides of dice
typeset _numDice ; integer _numDice=$2 # Number of dice to roll
typeset _rep ; typeset -l -L1 _rep="$3" # Report type: (sum || array)
 
typeset _seed ; (( _seed = SECONDS / $$ )) ; _seed=${_seed#*\.}
typeset _i _sum ; integer _i _sum=0
typeset _arr ; typeset -a _arr
 
RANDOM=${_seed}
for (( _i=0; _i<_numDice; _i++ )); do
(( _arr[_i] = (RANDOM % _sides) + 1 ))
[[ ${_rep} == s ]] && (( _sum += _arr[_i] ))
done
 
if [[ ${_rep} == s ]]; then
echo ${_sum}
else
echo "${_arr[@]}"
fi
}
 
# # Function _sumarr(n arr) - Return the sum of the first n arr elements
#
function _sumarr {
typeset _n ; integer _n=$1
typeset _arr ; nameref _arr="$2"
typeset _i _sum ; integer _i _sum
 
for ((_i=0; _i<_n; _i++)); do
(( _sum+=_arr[_i] ))
done
echo ${_sum}
}
 
######
# main #
######
 
until (( total >= MINTOT )) && (( cnt15 >= MIN15S )); do
integer total=0 cnt15=0
unset attrval ; typeset -A attrval
for attr in ${attribs[*]}; do
unset darr ; typeset -a darr=( $(_diceroll 6 4 a) )
set -sK:nr -A darr
attrval[${attr}]=$(_sumarr 3 darr)
(( total += attrval[${attr}] ))
(( attrval[${attr}] > 14 )) && (( cnt15++ ))
done
done
 
for attr in ${attribs[*]}; do
printf "%12s: %2d\n" ${attr} ${attrval[${attr}]}
done
print "Attribute value total: ${total}"
print "Attribule count >= 15: ${cnt15}"
</syntaxhighlight>
{{out}}<pre>
strength: 11
dexterity: 14
constitution: 14
intelligence: 15
wisdom: 15
charisma: 12
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}}==
<syntaxhighlight lang="mathematica">valid = False;
While[! valid,
try = Map[Total[TakeLargest[#, 3]] &,
RandomInteger[{1, 6}, {6, 4}]];
If[Total[try] > 75 && Count[try, _?(GreaterEqualThan[15])] >= 2,
valid = True;
]
]
{Total[try], try}</syntaxhighlight>
{{out}}
<pre>{78, {13, 15, 9, 13, 12, 16}}</pre>
 
=={{header|min}}==
{{works with|min|0.19.6}}
<syntaxhighlight lang="min">randomize ; Seed the rng with current timestamp.
 
; Implement some general operators we'll need that aren't in the library.
(() 0 shorten) :new
((new (over -> swons)) dip times nip) :replicate
(('' '' '') => spread if) :if?
((1 0 if?) concat map sum) :count
 
(5 random succ) :d6 ; Roll a 1d6.
('d6 4 replicate '< sort 3 take sum) :attr ; Roll an attribute.
('attr 6 replicate) :attrs ; Roll 6 attributes.
(sum 75 >=) :big? ; Is a set of attributes "big?"
(attrs (dup big?) () (pop attrs) () linrec) :big ; Roll a set of big attributes.
((15 >=) count 2 >=) :special? ; Is a set of atributes "special?"
(big (dup special?) () (pop big) () linrec) :stats ; Roll a set of big and special attributes.
 
stats puts "Total: " print! sum puts!</syntaxhighlight>
{{out}}
<pre>
(11 17 12 16 9 12)
Total: 77
</pre>
 
=={{header|MiniScript}}==
<langsyntaxhighlight MiniScriptlang="miniscript">roll = function()
results = [0, 0, 0, 0]
for i in range(0,3)
results[i] =.push ceil(rnd * 6)
end for
results.sort
Line 1,185 ⟶ 2,780:
end function
while true
attributes = [0,0,0,0,0,0]
attributes = []
reroll = true
gt15 = 0 // (how many attributes > 15)
for i in range(0,5)
while reroll
attributes.push roll
gt15 = 0
if attributes[i] > 15 then gt15 = gt15 + 1
for attrib in range(0,5)
attributes[attrib] = roll
if attributes[attrib] > 15 then gt15 = gt15 + 1
end for
print "Attribute values: " + attributes.join(", ")
print "AttributeAttributes Totalstotal: " + attributes.sum
if attributes.sum <>= 75 orand gt15 <>= 2 then break
print "Attributes failed, rerolling"
elseprint
print "Attributes succesful"
reroll = false
end if
end while
print "Success!"
</lang>
</syntaxhighlight>
{{out}}
<pre>Attribute values: 11, 13, 8, 10, 8, 10
<pre>
Attributes total: 60
Attribute values: 14, 12, 11, 8, 14, 10
Attribute Totals: 69
Attributes failed, rerolling
Attribute values: 10, 9, 7, 16, 12, 10
Attribute Totalsvalues: 6411, 13, 14, 13, 15, 14
Attributes total: 80
Attributes failed, rerolling
Attribute values: 13, 13, 18, 14, 13, 9
Attribute Totalsvalues: 8013, 11, 12, 9, 13, 12
Attributes total: 70
Attributes failed, rerolling
Attribute values: 3, 12, 12, 18, 18, 14
Attribute Totalsvalues: 7718, 17, 12, 10, 17, 12
Attributes succesfultotal: 86
Success!</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">
# Import "random" to get random numbers and "algorithm" to get sorting functions for arrays.
import random, algorithm
 
randomize()
 
proc diceFourRolls(): array[4, int] =
## Generates 4 random values between 1 and 6.
for i in 0 .. 3:
result[i] = rand(1..6)
 
proc sumRolls(rolls: array[4, int]): int =
## Save the sum of the 3 highest values rolled.
var sorted = rolls
sorted.sort()
# By sorting first and then starting the iteration on 1 instead of 0, the lowest number is discarded even if it is repeated.
for i in 1 .. 3:
result += sorted[i]
 
func twoFifteens(attr: var array[6, int]): bool =
attr.sort()
# Sorting implies than the second to last number is lesser than or equal to the last.
if attr[4] < 15: false else: true
 
var sixAttr: array[6, int]
 
while true:
var sumAttr = 0
for i in 0 .. 5:
sixAttr[i] = sumRolls(diceFourRolls())
sumAttr += sixAttr[i]
echo "The roll sums are, in order: ", sixAttr, ", which adds to ", sumAttr
if not twoFifteens(sixAttr) or sumAttr < 75: echo "Not good enough. Rerolling..."
else: break
</syntaxhighlight>
 
Sample output: <pre>
The roll sums are, in order: [8, 10, 16, 17, 10, 11], which adds to 72
Not good enough. Rerolling
The roll sums are, in order: [13, 13, 14, 13, 10, 16], which adds to 79
Not good enough. Rerolling
The roll sums are, in order: [12, 18, 16, 13, 17, 8], which adds to 84</pre>
 
=={{header|OCaml}}==
Original version by [http://rosettacode.org/wiki/User:Vanyamil User:Vanyamil]
<syntaxhighlight lang="ocaml">
(* Task : RPG_attributes_generator *)
 
(*
A programmatic solution to generating character attributes for an RPG
*)
 
(* Generates random whole values between 1 and 6. *)
let rand_die () : int = Random.int 6
 
(* Generates 4 random values and saves the sum of the 3 largest *)
let rand_attr () : int =
let four_rolls = [rand_die (); rand_die (); rand_die (); rand_die ()]
|> List.sort compare in
let three_best = List.tl four_rolls in
List.fold_left (+) 0 three_best
 
(* Generates a total of 6 values this way. *)
let rand_set () : int list=
[rand_attr (); rand_attr (); rand_attr ();
rand_attr (); rand_attr (); rand_attr ()]
 
(* Verifies conditions: total >= 75, at least 2 >= 15 *)
let rec valid_set () : int list=
let s = rand_set () in
let above_15 = List.fold_left (fun acc el -> if el >= 15 then acc + 1 else acc) 0 s in
let total = List.fold_left (+) 0 s in
if above_15 >= 2 && total >= 75
then s
else valid_set ()
 
(*** Output ***)
 
let _ =
let s = valid_set () in
List.iter (fun i -> print_int i; print_string ", ") s
</syntaxhighlight>
{{out}}
<pre>
11, 15, 11, 15, 14, 14,
</pre>
 
=={{header|Pascal|FreePascal}}==
 
<syntaxhighlight lang="pascal">
<lang Pascal>
program attributes;
 
Line 1,264 ⟶ 2,944:
writeln(' ---');
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,299 ⟶ 2,979:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use List::Util 'sum';
 
Line 1,318 ⟶ 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 1,327 ⟶ 3,007:
Cha = 10
Sum = 83, with 3 attributes >= 15</pre>
 
=={{header|Perl 6}}==
{{works with|Rakudo Star|2018.04.1}}
<lang perl6>my ( $min_sum, $hero_attr_min, $hero_count_min ) = 75, 15, 2;
my @attr-names = <Str Int Wis Dex Con Cha>;
 
sub heroic { + @^a.grep: * >= $hero_attr_min }
 
my @attr;
repeat until @attr.sum >= $min_sum
and heroic(@attr) >= $hero_count_min {
 
@attr = @attr-names.map: { (1..6).roll(4).sort(+*).skip(1).sum };
}
 
say @attr-names Z=> @attr;
say "Sum: {@attr.sum}, with {heroic(@attr)} attributes >= $hero_attr_min";</lang>
{{out}}
<pre>
(Str => 15 Int => 16 Wis => 13 Dex => 11 Con => 15 Cha => 6)
Sum: 76, with 3 attributes >= 15
</pre>
 
=={{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 1,377 ⟶ 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===
<lang php><?php
<syntaxhighlight lang="php"><?php
 
$attributesTotal = 0;
Line 1,409 ⟶ 3,111:
}
 
print_r($attributes);</langsyntaxhighlight>
 
===Version 2===
<syntaxhighlight lang="php"><?php
class CharacterGenerator {
public static function roll(): array
{
$attributes = array_map(fn($stat) => self::rollStat(), range(1, 6));
if (!self::meetsRequirements($attributes)) {
return self::roll();
}
return $attributes;
}
private static function rollStat(): int
{
$rolls = d(6, 4);
return array_sum($rolls) - min($rolls);
}
private static function meetsRequirements(array $attributes): bool
{
$twoOrMoreOverFifteen = array_reduce($attributes, fn($n, $stat) => $n + ($stat > 15)) >= 2;
$sumOfAttributesMeetsMinimum = array_sum($attributes) >= 75;
return $sumOfAttributesMeetsMinimum && $twoOrMoreOverFifteen;
}
}
function d(int $d, int $numberToRoll): array
{
return array_map(fn($roll) => rand(1, $d), range(1, $numberToRoll));
}
$characterAttributes = CharacterGenerator::roll();
$attributesString = implode(', ', $characterAttributes);
$attributesTotal = array_sum($characterAttributes);
print "Attribute Total: $attributesTotal\n";
print "Attributes: $attributesString";</syntaxhighlight>
 
{{out}}
<pre>Attribute Total: 80
Attributes: 10, 12, 16, 17, 14, 11</pre>
 
=={{header|Plain English}}==
<syntaxhighlight lang="plainenglish">To add an attribute to some stats:
Allocate memory for an entry.
Put the attribute into the entry's attribute.
Append the entry to the stats.
 
An attribute is a number.
 
An entry is a thing with an attribute.
 
To find a count of attributes greater than fourteen in some stats:
Put 0 into the count.
Get an entry from the stats.
Loop.
If the entry is nil, exit.
If the entry's attribute is greater than 14, bump the count.
Put the entry's next into the entry.
Repeat.
 
To find a sum of some stats:
Put 0 into the sum.
Get an entry from the stats.
Loop.
If the entry is nil, exit.
Add the entry's attribute to the sum.
Put the entry's next into the entry.
Repeat.
 
To generate an attribute:
Put 0 into the attribute.
Put 6 into a minimum number.
Loop.
If a counter is past 4, break.
Pick a number between 1 and 6.
Add the number to the attribute.
If the number is less than the minimum, put the number into the minimum.
Repeat.
Subtract the minimum from the attribute.
 
To generate some stats (raw):
If a counter is past 6, exit.
Generate an attribute.
Add the attribute to the stats.
Repeat.
 
To generate some stats (valid):
Generate some raw stats (raw).
Find a sum of the raw stats.
If the sum is less than 75, destroy the raw stats; repeat.
Find a count of attributes greater than fourteen in the raw stats.
If the count is less than 2, destroy the raw stats; repeat.
Put the raw stats into the stats.
 
To run:
Start up.
Show some RPG attributes.
Wait for the escape key.
Shut down.
 
To show some RPG attributes:
Generate some stats (valid).
Find a sum of the stats.
Find a count of attributes greater than fourteen in the stats.
Write the stats on the console.
Destroy the stats.
Write "Total: " then the sum on the console.
Write "Number of stats greater than fourteen: " then the count on the console.
 
Some stats are some entries.
 
A sum is a number.
 
To write some stats on the console:
Get an entry from the stats.
Loop.
If the entry is nil, write "" on the console; exit.
Convert the entry's attribute to a string.
Write the string 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.
Repeat.</syntaxhighlight>
{{out}}
<pre>
13, 10, 17, 10, 18, 14
Total: 82
Number of stats greater than fourteen: 2
</pre>
 
=={{header|PureBasic}}==
<langsyntaxhighlight lang="purebasic">#heroicAttributeMinimum = 15
#heroicAttributeCountMinimum = 2
#attributeSumMinimum = 75
Line 1,467 ⟶ 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 1,474 ⟶ 3,311:
=={{header|Python}}==
===Python: Simple===
<langsyntaxhighlight lang="python">import random
random.seed()
attributes_total = 0
Line 1,500 ⟶ 3,337:
attributes_total = sum(attributes)
print(attributes_total, attributes)</langsyntaxhighlight>
 
{{out}}
Line 1,507 ⟶ 3,344:
 
===Python: Nested Comprehensions #1===
<langsyntaxhighlight lang="python">import random
random.seed()
total = 0
Line 1,521 ⟶ 3,358:
total = sum(attributes)
print(total, attributes)</langsyntaxhighlight>
 
{{out}}
Line 1,529 ⟶ 3,366:
===Python: Nested Comprehensions #2===
With comprehensions for checking candidate values in the while expression.
<langsyntaxhighlight lang="python">import random
 
def compute():
Line 1,540 ⟶ 3,377:
for i in range(3):
print(*compute())
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,550 ⟶ 3,387:
Composing a hero-generator from reusable functions:
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''RPG Attributes Generator'''
 
from itertools import islice
from random import randint
from operator import eq
import random
 
 
Line 1,560 ⟶ 3,397:
def heroes(p):
'''Non-finite list of heroes matching
the requirements of predicate p.'''
'''
while True:
yield herotuple(p)
until(p)(character)([])
 
)
 
# hero :: ([Int] -> Bool) -> IO (Int, Int, Int, Int, Int, Int)
def hero(p):
'''A random character matching the
requirements of predicate p.'''
return tuple(
until(p)(character)([])
)
 
 
Line 1,577 ⟶ 3,408:
def character(_):
'''A random character with six
integral attributes.'''
'''
return [
sum(sorted(map(
Line 1,587 ⟶ 3,419:
 
 
# TEST ------------------------- TEST --------------------------
# main :: IO ()
def main():
Line 1,595 ⟶ 3,427:
def seventyFivePlusIncTwo15s(xs):
'''Sums to 75 or more,
and includes at least two 15s.'''
'''
return 75 <= sum(xs) and (
1 < len(list(filter(curry(eq)(15), xs)))
Line 1,609 ⟶ 3,442:
 
 
# GENERIC ------------------------- GENERIC -------------------------
 
# curry :: ((a, b) -> c) -> a -> b -> c
def curry(f):
'''A curried function derived
from an uncurried function.'''
'''
return lambda a: lambda b: f(a, b)
return lambda x: lambda y: f(x, y)
 
 
# enumFromTo :: (Int, -> Int) -> [Int]
def enumFromTo(m):
'''IntegerEnumeration enumerationof frominteger mvalues to n[m..n]'''
return lambda n: list(range(m, 1 + n))
 
 
# randomRInt :: Int -> Int -> IO () -> Int
def randomRInt(m):
'''The return value of randomRInt is itself
'''Returns a generator function
whicha canfunction. beThe appliedreturned tofunction, any argument,whenever
called, yields a a new pseudo-random integer
always returning some integer in
in the range [m to ..n].'''
'''
return lambda n: lambda _: random.randint(m, n)
return lambda n: lambda _: randint(m, n)
 
 
Line 1,637 ⟶ 3,472:
def take(n):
'''The prefix of xs of length n,
or xs itself if n > length xs.'''
'''
return lambda xs: (
xs[0:n]
if isinstance(xs, (list, tuple))
else list(islice(xs, n))
)
Line 1,647 ⟶ 3,483:
# unlines :: [String] -> String
def unlines(xs):
'''A single string derivedformed by the intercalation
of a list of strings with the newline character.'''
'''
return '\n'.join(xs)
 
Line 1,655 ⟶ 3,492:
def until(p):
'''The result of repeatedly applying f until p holds.
The initial seed value is x.'''
'''
def go(f, x):
v = x
Line 1,665 ⟶ 3,503:
 
if __name__ == '__main__':
main()</langsyntaxhighlight>
<pre>A sample of 10:
 
Line 1,678 ⟶ 3,516:
79 -> (17, 15, 10, 11, 15, 11)
75 -> (15, 13, 7, 11, 14, 15)</pre>
 
===Python: One-liner===
Just because you can, doesn't mean you should.
<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)])))
</syntaxhighlight>
<pre>
Attributes: [16, 15, 15, 14, 8, 10]
Total: 78
</pre>
 
=={{header|Quackery}}==
<syntaxhighlight lang="quackery">[ 0 swap witheach + ] is sum ( [ --> n )
 
[ 0 ]'[ rot witheach
[ over do swap dip + ] drop ] is count ( [ --> n )
 
[ [] 4 times
[ 6 random 1+ join ]
sort behead drop sum ] is attribute ( --> n )
 
[ [] 6 times
[ attribute join ] ] is raw ( --> [ )
 
[ dup sum 74 > not iff
[ drop false ] done
count [ 14 > ] 1 > ] is valid ( [ --> b )
 
[ raw dup valid if
done
drop again ] is stats ( --> [ )
 
randomise
stats dup echo cr cr
say 'Sum: ' dup sum echo cr
say '# of attributes > 14: '
count [ 14 > ] echo</syntaxhighlight>
{{out}}
<pre>
[ 10 16 16 8 12 15 ]
 
Sum: 77
# of attributes > 14: 3
</pre>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
 
(define (d6 . _)
Line 1,697 ⟶ 3,579:
(module+ main
(define-values (rolled-stats total) (generate-character))
(printf "Rolls:\t~a~%Total:\t~a" rolled-stats total))</langsyntaxhighlight>
 
{{out}}
<pre>Rolls: (11 16 10 13 12 15)
Total: 77</pre>
 
=={{header|R}}==
The base library already has an attributes function, so we avoid using that name. Otherwise, this is R's bread and butter.
<syntaxhighlight lang="rsplus">genStats <- function()
{
stats <- c(STR = 0, DEX = 0, CON = 0, INT = 0, WIS = 0, CHA = 0)
for(i in seq_along(stats))
{
results <- sample(6, 4, replace = TRUE)
stats[i] <- sum(results[-which.min(results)])
}
if(sum(stats >= 15) < 2 || (stats["TOT"] <- sum(stats)) < 75) Recall() else stats
}
print(genStats())</syntaxhighlight>
 
{{out}}
<pre>STR DEX CON INT WIS CHA TOT
15 18 18 11 15 15 92 </pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo Star|2018.04.1}}
<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>;
 
sub heroic { + @^a.grep: * >= $hero_attr_min }
 
my @attr;
repeat until @attr.sum >= $min_sum
and heroic(@attr) >= $hero_count_min {
 
@attr = @attr-names.map: { (1..6).roll(4).sort(+*).skip(1).sum };
}
 
say @attr-names Z=> @attr;
say "Sum: {@attr.sum}, with {heroic(@attr)} attributes >= $hero_attr_min";</syntaxhighlight>
{{out}}
<pre>
(Str => 15 Int => 16 Wis => 13 Dex => 11 Con => 15 Cha => 6)
Sum: 76, with 3 attributes >= 15
</pre>
 
=={{header|Red}}==
<syntaxhighlight lang="rebol">Red ["RPG attributes generator"]
 
raw-attribute: does [
sum next sort collect [
loop 4 [keep random/only 6]
]
]
 
raw-attributes: does [
collect [
loop 6 [keep raw-attribute]
]
]
 
valid-attributes?: func [b][
n: 0
foreach attr b [
if attr > 14 [n: n + 1]
]
all [
n > 1
greater? sum b 74
]
]
 
attributes: does [
until [
valid-attributes? a: raw-attributes
]
a
]
 
show-attributes: function [a][
i: 1
foreach stat-name [
"Strength"
"Dexterity"
"Constitution"
"Intelligence"
"Wisdom"
"Charisma"
][
print [rejoin [stat-name ":"] a/:i]
i: i + 1
]
print "-----------------"
print ["Sum:" sum a]
print [n "attributes > 14"]
]
 
show-attributes attributes</syntaxhighlight>
{{out}}
<pre>
Strength: 14
Dexterity: 11
Constitution: 16
Intelligence: 9
Wisdom: 16
Charisma: 16
-----------------
Sum: 82
3 attributes > 14
</pre>
 
=={{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 1,768 ⟶ 3,756:
swl=swl wa.i
End
Return strip(swl)</langsyntaxhighlight>
{{out}}
<pre>I:\>rexx cast
Line 1,779 ⟶ 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 1,792 ⟶ 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 1,800 ⟶ 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 1,819 ⟶ 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 1,828 ⟶ 3,816:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : RPG Attributes Generator
 
Line 1,873 ⟶ 3,861:
line = line + "])"
see line + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,880 ⟶ 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 1,890 ⟶ 3,878:
p res
puts "sum: #{res.sum}"
</syntaxhighlight>
</lang>
{{out}}
<pre>[12, 14, 17, 12, 16, 9]
sum: 80
</pre>
 
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">dim statnames$(6)
data "STR", "CON", "DEX", "INT", "WIS", "CHA"
for i = 1 to 6
read statnames$(i)
next i
dim stat(6)
acceptable = false
 
while 1
sum = 0 : n15 = 0
for i = 1 to 6
stat(i) = rollstat()
sum = sum + stat(i)
if stat(i) >= 15 then n15 = n15 + 1
next i
if sum >= 75 and n15 >= 2 then exit while
wend
 
for i = 1 to 6
print statnames$(i); ": "; stat(i)
next i
print "-------"
print "TOT: "; sum
end
 
function d6()
d6 = 1 + int(rnd(1) * 6)
end function
 
function rollstat()
a = d6() : b = d6() : c = d6() : d = d6()
rollstat = a + b + c + d - min(min(a, b), min(c, d))
end function</syntaxhighlight>
 
=={{header|Rust}}==
Line 1,900 ⟶ 3,923:
{{libheader|rand}}
{{works with|Rust|2018}}
<langsyntaxhighlight lang="rust">
use rand::distributions::Uniform;
use rand::prelude::{thread_rng, ThreadRng};
Line 1,977 ⟶ 4,000:
}
}
</syntaxhighlight>
</lang>
{{out}}
Sample output, running the generator ten times:
Line 1,995 ⟶ 4,018:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">
import scala.util.Random
Random.setSeed(1)
Line 2,016 ⟶ 4,039:
 
println("picked => " + getCharacter.mkString("[", ",", "]"))
</syntaxhighlight>
</lang>
 
<pre>
Line 2,027 ⟶ 4,050:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const proc: main is func
Line 2,064 ⟶ 4,087:
writeln(" ----");
writeln("Total " <& total lpad 3);
end func;</langsyntaxhighlight>
 
{{out}}
Line 2,078 ⟶ 4,101:
Total 76
</pre>
 
=={{header|True BASIC}}==
{{trans|FreeBASIC}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">FUNCTION min(a, b)
IF a < b THEN LET min = a ELSE LET min = b
END FUNCTION
 
FUNCTION d6
LET d6 = 1 + INT(RND * 6)
END FUNCTION
 
FUNCTION rollstat
LET a = d6
LET b = d6
LET c = d6
LET d = d6
LET rollstat = a + b + c + d - min(min(a, b), min(c, d))
END FUNCTION
 
DIM statnames$(6)
DATA "STR", "CON", "DEX", "INT", "WIS", "CHA"
FOR i = 1 TO 6
READ statnames$(i)
NEXT i
DIM stat(6)
LET acceptable = 0
 
RANDOMIZE ! RANDOMIZE TIMER en QBasic
DO
LET sum = 0
LET n15 = 0
FOR i = 1 to 6
LET stat(i) = rollstat
LET sum = sum + stat(i)
IF stat(i) >= 15 THEN LET n15 = n15 + 1
NEXT i
IF sum >= 75 AND n15 >= 2 THEN LET acceptable = 1
LOOP UNTIL acceptable = 1
 
FOR i = 1 to 6
PRINT statnames$(i); ": "; stat(i)
NEXT i
PRINT "--------"
PRINT "TOT: "; sum
END</syntaxhighlight>
{{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}}
{{works with|Korn Shell}}
{{works with|Zsh}}
 
<syntaxhighlight lang="sh">function main {
typeset attrs=(str dex con int wis cha)
typeset -A values
typeset attr
typeset -i value total fifteens
while true; do
fifteens=0
total=0
for attr in "${attrs[@]}"; do
# "random" values repeat in zsh if run in a subshell
r4d6drop >/tmp/$$
read value </tmp/$$
values[$attr]=$value
(( total += value ))
if (( value >= 15 )); then
(( fifteens += 1 ))
fi
done
if (( total >= 75 && fifteens >= 2 )); then
break
fi
done
rm -f /tmp/$$
for attr in "${attrs[@]}"; do
printf '%s: %d\n' "$attr" "${values[$attr]}"
done
}
 
function r4d6drop {
typeset -i d1=RANDOM%6+1 d2=RANDOM%6+1 d3=RANDOM%6+1 d4=RANDOM%6+1
typeset e=$(printf '%s\n' $d1 $d2 $d3 $d4 |
sort -n | tail -n +2 | tr $'\n' +)
printf '%d\n' $(( ${e%+} ))
}
 
main "$@"
</syntaxhighlight>
{{Out}}
<pre>str: 12
dex: 15
con: 15
int: 14
wis: 8
cha: 12</pre>
 
=={{header|Visual Basic .NET}}==
repeats until a successful outcome occurs
<langsyntaxhighlight lang="vbnet">Module Module1
 
Dim r As New Random
Line 2,106 ⟶ 4,274:
Loop Until good
End Sub
End Module</langsyntaxhighlight>
{{out}}
sample outputs:
Line 2,120 ⟶ 4,288:
attribs: 17, 16, 14, 8, 8, 9, sum=72, (low sum, high vals=2) - failure
attribs: 18, 16, 13, 9, 9, 10, sum=75, (good sum, high vals=2) - success</pre>
 
=={{header|Wren}}==
{{libheader|Wren-sort}}
<syntaxhighlight lang="wren">import "random" for Random
import "./sort" for Sort
 
var rand = Random.new()
var vals = List.filled(6, 0)
while (true) {
for (i in 0..5) {
var rns = List.filled(4, 0)
for (j in 0..3) rns[j] = rand.int(1, 7)
var sum = rns.reduce { |acc, n| acc + n }
Sort.insertion(rns)
vals[i] = sum - rns[0]
}
var total = vals.reduce { |acc, n| acc + n }
if (total >= 75) {
var fifteens = vals.count { |n| n >= 15 }
if (fifteens >= 2) {
System.print("The six values are: %(vals)")
System.print("Their total is: %(total)")
break
}
}
}</syntaxhighlight>
 
{{out}}
Sample run:
<pre>
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}}
<syntaxhighlight lang="freebasic">sub d6()
//simulates a marked regular hexahedron coming to rest on a plane
return 1 + int(ran(6))
end sub
 
sub roll_stat()
//rolls four dice, returns the sum of the three highest
a = d6() : b = d6(): c = d6(): d = d6()
return a + b + c + d - min(min(a, b), min(c, d))
end sub
 
dim statnames$(6)
statnames$(1) = "STR"
statnames$(2) = "CON"
statnames$(3) = "DEX"
statnames$(4) = "INT"
statnames$(5) = "WIS"
statnames$(6) = "CHA"
 
dim stat(6)
acceptable = false
 
repeat
sum = 0
n15 = 0
for i = 1 to 6
stat(i) = roll_stat()
sum = sum + stat(i)
if stat(i) >= 15 then n15 = n15 + 1 : fi
next i
if sum >= 75 and n15 >= 2 then acceptable = true : fi
until acceptable
 
for i = 1 to 6
print statnames$(i), ": ", stat(i) using "##"
next i
print "-------\nTOT: ", sum
end</syntaxhighlight>
{{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 2,130 ⟶ 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>
2,043

edits