RPG attributes generator: Difference between revisions
Add Uiua |
|||
Line 1,791: | Line 1,791: | ||
func rollstat . |
func rollstat . |
||
for i to 4 |
for i to 4 |
||
h = |
h = random 6 |
||
s += h |
s += h |
||
min = lower min h |
min = lower min h |
Revision as of 18:28, 17 July 2024
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
RPG = Role Playing Game.
You're running a tabletop RPG, and your players are creating characters.
Each character has six core attributes: strength, dexterity, constitution, intelligence, wisdom, and charisma.
One way of generating values for these attributes is to roll four, 6-sided dice (d6) and sum the three highest rolls, discarding the lowest roll.
Some players like to assign values to their attributes in the order they're rolled.
To ensure generated characters don't put players at a disadvantage, the following requirements must be satisfied:
- The total of all character attributes must be at least 75.
- At least two of the attributes must be at least 15.
However, this can require a lot of manual dice rolling. A programatic solution would be much faster.
- Task
Write a program that:
- Generates 4 random, whole values between 1 and 6.
- Saves the sum of the 3 largest values.
- Generates a total of 6 values this way.
- Displays the total, and all 6 values once finished.
- The order in which each value was generated must be preserved.
- The total of all 6 values must be at least 75.
- At least 2 of the values must be 15 or more.
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)
- Output:
76 [13, 13, 11, 14, 8, 17]
8086 Assembly
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
- Output:
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
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"
- Output:
Value = 11 Value = 13 Value = 16 Value = 11 Value = 14 Value = 18 Total = 83 Values above 15 = 2
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
- Output:
Screenshot from Atari 8-bit computer
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
ALGOL 68
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
- Output:
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
APL
roll←{(+/-⌊/)¨?¨6/⊂4/6}⍣{(75≤+/⍺)∧2≤+/⍺≥15}
- Output:
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
ARM Assembly
/* ARM assembly Raspberry PI */
/* program rpg.s */
/************************************/
/* Constantes */
/************************************/
.equ STDOUT, 1 @ Linux output console
.equ EXIT, 1 @ Linux syscall
.equ WRITE, 4 @ Linux syscall
.equ NBTIRAGES, 4
.equ NBTIRAGESOK, 3
.equ NBVALUES, 6
.equ TOTALMIN, 75
.equ MAXVALUE, 15
.equ NBMAXVALUE, 2
/*******************************************/
/* Fichier des macros */
/********************************************/
.include "../../ficmacros.s"
/*********************************/
/* Initialized data */
/*********************************/
.data
sMessResult: .ascii "Value = "
sMessValeur: .fill 11, 1, ' ' @ size => 11
szCarriageReturn: .asciz "\n"
sMessResultT: .ascii "Total = "
sMessValeurT: .fill 11, 1, ' ' @ size => 11
.asciz "\n"
sMessResultQ: .ascii "Values above 15 = "
sMessValeurQ: .fill 11, 1, ' ' @ size => 11
.asciz "\n"
.align 4
iGraine: .int 123456
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
tiTirages: .skip 4 * NBTIRAGES
tiValues: .skip 4 * NBVALUES
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: @ entry of program
1: @ begin loop 1
mov r2,#0 @ counter value >15
mov r4,#0 @ loop indice
mov r5,#0 @ total
ldr r3,iAdrtiValues @ table values address
2:
bl genValue @ call generate value
str r0,[r3,r4,lsl #2] @ store in table
add r5,r0 @ compute total
cmp r0,#MAXVALUE @ count value > 15
addge r2,#1
add r4,#1 @ increment indice
cmp r4,#NBVALUES @ end ?
blt 2b
cmp r5,#TOTALMIN @ compare 75
blt 1b @ < loop
cmp r2,#NBMAXVALUE @ compare value > 15
blt 1b @ < loop
ldr r0,iAdrtiValues @ display values
bl displayTable
mov r0,r5 @ total
ldr r1,iAdrsMessValeurT @ display value
bl conversion10 @ call conversion decimal
ldr r0,iAdrsMessResultT
bl affichageMess @ display message
mov r0,r2 @ counter value > 15
ldr r1,iAdrsMessValeurQ @ display value
bl conversion10 @ call conversion decimal
ldr r0,iAdrsMessResultQ
bl affichageMess @ display message
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc #0 @ perform the system call
iAdrsMessValeur: .int sMessValeur
iAdrszCarriageReturn: .int szCarriageReturn
iAdrsMessResult: .int sMessResult
iAdrsMessValeurT: .int sMessValeurT
iAdrsMessResultT: .int sMessResultT
iAdrsMessValeurQ: .int sMessValeurQ
iAdrsMessResultQ: .int sMessResultQ
iAdrtiValues: .int tiValues
/******************************************************************/
/* generate value */
/******************************************************************/
/* r0 returns the value */
genValue:
push {r1-r4,lr} @ save registers
mov r4,#0 @ indice loop
ldr r1,iAdrtiTirages @ table tirage address
1:
mov r0,#6
bl genereraleas @ result 0 to 5
add r0,#1 @ for 1 to 6
str r0,[r1,r4,lsl #2] @ store tirage
add r4,#1 @ increment indice
cmp r4,#NBTIRAGES @ end ?
blt 1b @ no -> loop
ldr r0,iAdrtiTirages @ table tirage address
mov r1,#0 @ first item
mov r2,#NBTIRAGES @ number of tirages
bl shellSort @ sort table decreasing
mov r4,#0 @ raz indice loop
mov r0,#0 @ total
ldr r1,iAdrtiTirages @ table tirage address
2:
ldr r2,[r1,r4,lsl #2] @ read tirage
add r0,r2 @ compute sum
add r4,#1 @ inrement indice
cmp r4,#NBTIRAGESOK @ end ?
blt 2b
100:
pop {r1-r4,lr}
bx lr @ return
iAdrtiTirages: .int tiTirages
/***************************************************/
/* shell Sort decreasing */
/***************************************************/
/* r0 contains the address of table */
/* r1 contains the first element but not use !! */
/* this routine use first element at index zero !!! */
/* r2 contains the number of element */
shellSort:
push {r0-r7,lr} @save registers
sub r2,#1 @ index last item
mov r1,r2 @ init gap = last item
1: @ start loop 1
lsrs r1,#1 @ gap = gap / 2
beq 100f @ if gap = 0 -> end
mov r3,r1 @ init loop indice 1
2: @ start loop 2
ldr r4,[r0,r3,lsl #2] @ load first value
mov r5,r3 @ init loop indice 2
3: @ start loop 3
cmp r5,r1 @ indice < gap
blt 4f @ yes -> end loop 2
sub r6,r5,r1 @ index = indice - gap
ldr r7,[r0,r6,lsl #2] @ load second value
cmp r4,r7 @ compare values
strgt r7,[r0,r5,lsl #2] @ store if >
subgt r5,r1 @ indice = indice - gap
bgt 3b @ and loop
4: @ end loop 3
str r4,[r0,r5,lsl #2] @ store value 1 at indice 2
add r3,#1 @ increment indice 1
cmp r3,r2 @ end ?
ble 2b @ no -> loop 2
b 1b @ yes loop for new gap
100: @ end function
pop {r0-r7,lr} @ restaur registers
bx lr @ return
/******************************************************************/
/* Display table elements */
/******************************************************************/
/* r0 contains the address of table */
displayTable:
push {r0-r3,lr} @ save registers
mov r2,r0 @ table address
mov r3,#0
1: @ loop display table
ldr r0,[r2,r3,lsl #2]
ldr r1,iAdrsMessValeur @ display value
bl conversion10 @ call function
ldr r0,iAdrsMessResult
bl affichageMess @ display message
add r3,#1
cmp r3,#NBVALUES - 1
ble 1b
ldr r0,iAdrszCarriageReturn
bl affichageMess
100:
pop {r0-r3,lr}
bx lr
/******************************************************************/
/* display text with size calculation */
/******************************************************************/
/* r0 contains the address of the message */
affichageMess:
push {r0,r1,r2,r7,lr} @ save registres
mov r2,#0 @ counter length
1: @ loop length calculation
ldrb r1,[r0,r2] @ read octet start position + index
cmp r1,#0 @ if 0 its over
addne r2,r2,#1 @ else add 1 in the length
bne 1b @ and loop
@ so here r2 contains the length of the message
mov r1,r0 @ address message in r1
mov r0,#STDOUT @ code to write to the standard output Linux
mov r7, #WRITE @ code call system "write"
svc #0 @ call systeme
pop {r0,r1,r2,r7,lr} @ restaur des 2 registres */
bx lr @ return
/******************************************************************/
/* Converting a register to a decimal unsigned */
/******************************************************************/
/* r0 contains value and r1 address area */
/* r0 return size of result (no zero final in area) */
/* area size => 11 bytes */
.equ LGZONECAL, 10
conversion10:
push {r1-r4,lr} @ save registers
mov r3,r1
mov r2,#LGZONECAL
1: @ start loop
bl divisionpar10U @ unsigned r0 <- dividende. quotient ->r0 reste -> r1
add r1,#48 @ digit
strb r1,[r3,r2] @ store digit on area
cmp r0,#0 @ stop if quotient = 0
subne r2,#1 @ else previous position
bne 1b @ and loop
@ and move digit from left of area
mov r4,#0
2:
ldrb r1,[r3,r2]
strb r1,[r3,r4]
add r2,#1
add r4,#1
cmp r2,#LGZONECAL
ble 2b
@ and move spaces in end on area
mov r0,r4 @ result length
mov r1,#' ' @ space
3:
strb r1,[r3,r4] @ store space in area
add r4,#1 @ next position
cmp r4,#LGZONECAL
ble 3b @ loop if r4 <= area size
100:
pop {r1-r4,lr} @ restaur registres
bx lr @return
/***************************************************/
/* division par 10 unsigned */
/***************************************************/
/* r0 dividende */
/* r0 quotient */
/* r1 remainder */
divisionpar10U:
push {r2,r3,r4, lr}
mov r4,r0 @ save value
//mov r3,#0xCCCD @ r3 <- magic_number lower raspberry 3
//movt r3,#0xCCCC @ r3 <- magic_number higter raspberry 3
ldr r3,iMagicNumber @ r3 <- magic_number raspberry 1 2
umull r1, r2, r3, r0 @ r1<- Lower32Bits(r1*r0) r2<- Upper32Bits(r1*r0)
mov r0, r2, LSR #3 @ r2 <- r2 >> shift 3
add r2,r0,r0, lsl #2 @ r2 <- r0 * 5
sub r1,r4,r2, lsl #1 @ r1 <- r4 - (r2 * 2) = r4 - (r0 * 10)
pop {r2,r3,r4,lr}
bx lr @ leave function
iMagicNumber: .int 0xCCCCCCCD
/***************************************************/
/* Generation random number */
/***************************************************/
/* r0 contains limit */
genereraleas:
push {r1-r4,lr} @ save registers
ldr r4,iAdriGraine
ldr r2,[r4]
ldr r3,iNbDep1
mul r2,r3,r2
ldr r3,iNbDep1
add r2,r2,r3
str r2,[r4] @ maj de la graine pour appel suivant
cmp r0,#0
beq 100f
mov r1,r0 @ divisor
mov r0,r2 @ dividende
bl division
mov r0,r3 @ résult = remainder
100: @ end function
pop {r1-r4,lr} @ restaur registers
bx lr @ return
/*****************************************************/
iAdriGraine: .int iGraine
iNbDep1: .int 0x343FD
iNbDep2: .int 0x269EC3
/***************************************************/
/* integer division unsigned */
/***************************************************/
division:
/* r0 contains dividend */
/* r1 contains divisor */
/* r2 returns quotient */
/* r3 returns remainder */
push {r4, lr}
mov r2, #0 @ init quotient
mov r3, #0 @ init remainder
mov r4, #32 @ init counter bits
b 2f
1: @ loop
movs r0, r0, LSL #1 @ r0 <- r0 << 1 updating cpsr (sets C if 31st bit of r0 was 1)
adc r3, r3, r3 @ r3 <- r3 + r3 + C. This is equivalent to r3 ? (r3 << 1) + C
cmp r3, r1 @ compute r3 - r1 and update cpsr
subhs r3, r3, r1 @ if r3 >= r1 (C=1) then r3 <- r3 - r1
adc r2, r2, r2 @ r2 <- r2 + r2 + C. This is equivalent to r2 <- (r2 << 1) + C
2:
subs r4, r4, #1 @ r4 <- r4 - 1
bpl 1b @ if r4 >= 0 (N=0) then loop
pop {r4, lr}
bx lr
Arturo
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]
Atari 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!"
- Output:
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
BASIC256
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
- Output:
Igual que la entrada de FreeBASIC.
BQN
Slightly different from the APL solution primarily due to builtin differences.
_while_←{𝔽⍟𝔾∘𝔽_𝕣_𝔾∘𝔽⍟𝔾𝕩}
(2-modifier block)
Roll←{𝕊:{𝕊:((+´-⌊´)1+4⊸•rand.Range)¨6⥊6}_while_{(75>+´𝕩)∨2>+´15≤𝕩}⟨⟩}
(function block)
Roll@
⟨ 11 16 12 10 16 13 ⟩
C
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int compareInts(const void *i1, const void *i2) {
int a = *((int *)i1);
int b = *((int *)i2);
return a - b;
}
int main() {
int i, j, nsum, vsum, vcount, values[6], numbers[4];
srand(time(NULL));
for (;;) {
vsum = 0;
for (i = 0; i < 6; ++i) {
for (j = 0; j < 4; ++j) {
numbers[j] = 1 + rand() % 6;
}
qsort(numbers, 4, sizeof(int), compareInts);
nsum = 0;
for (j = 1; j < 4; ++j) {
nsum += numbers[j];
}
values[i] = nsum;
vsum += values[i];
}
if (vsum < 75) continue;
vcount = 0;
for (j = 0; j < 6; ++j) {
if (values[j] >= 15) vcount++;
}
if (vcount < 2) continue;
printf("The 6 random numbers generated are:\n");
printf("[");
for (j = 0; j < 6; ++j) printf("%d ", values[j]);
printf("\b]\n");
printf("\nTheir sum is %d and %d of them are >= 15\n", vsum, vcount);
break;
}
return 0;
}
- Output:
Sample run:
The 6 random numbers generated are: [9 15 15 17 13 8] Their sum is 77 and 3 of them are >= 15
C#
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);
}
}
- Output:
sample outputs:
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
attribs: 16, 8, 9, 15, 16, 12, sum=76, (good sum, high vals=3) - success
C++
GCC 4.9.2, unoptimised.
#include <algorithm>
#include <ctime>
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
srand(time(0));
unsigned int attributes_total = 0;
unsigned int count = 0;
int attributes[6] = {};
int rolls[4] = {};
while(attributes_total < 75 || count < 2)
{
attributes_total = 0;
count = 0;
for(int attrib = 0; attrib < 6; attrib++)
{
for(int roll = 0; roll < 4; roll++)
{
rolls[roll] = 1 + (rand() % 6);
}
sort(rolls, rolls + 4);
int roll_total = rolls[1] + rolls[2] + rolls[3];
attributes[attrib] = roll_total;
attributes_total += roll_total;
if(roll_total >= 15) count++;
}
}
cout << "Attributes generated : [";
cout << attributes[0] << ", ";
cout << attributes[1] << ", ";
cout << attributes[2] << ", ";
cout << attributes[3] << ", ";
cout << attributes[4] << ", ";
cout << attributes[5];
cout << "]\nTotal: " << attributes_total;
cout << ", Values above 15 : " << count;
return 0;
}
- Output:
Sample run:
Attributes generated : [13, 13, 17, 14, 10, 16] Total: 83, Values above 15 : 2
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.",!
; loop until valid result
do {
; loop through 6 attributes
for i = 1:1:6 {
set (low, dice, keep) = ""
; roll 4 dice each time
for j = 1:1:4 {
set roll = $r(6) + 1
set dice = dice + roll
if (roll < low) || (low = "") {
set low = roll
}
} ; 4 dice rolls per attribute
set keep = (dice - low)
set $list(attr,i) = keep
} ; 6 attributes
; loop the ending list
set (tot,bigs) = 0
for i = 1:1:$listlength(attr) {
set cur = $listget(attr,i)
set tot = tot + cur
if (cur >= 15) {
set bigs = bigs + 1
}
}
; analyze results
write !,"Scores: "_$lts(attr)
set result = $select((tot < 75) && (bigs < 2):0,tot < 75:1,bigs < 2:2,1:3)
write !,?5,$case(result,
0:"Total "_tot_" too small and not enough attributes ("_bigs_") >= 15.",
1:"Total "_tot_" too small.",
2:"Need 2 or more attributes >= 15, only have "_bigs_".",
3:"Total "_tot_" and "_bigs_ " attributes >=15 are sufficient.")
} while (result '= 3)
quit
- Output:
SAMPLES>do ^RPGGEN Rules: 1.) Total of 6 attributes must be at least 75. 2.) At least two scores must be 15 or more.
Scores: 13,14,12,14,16,14
Need 2 or more attributes >= 15, only have 1.Scores: 9,16,13,14,12,16
Total 80 and 2 attributes >=15 are sufficient.
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
- Output:
$ ./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
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.)
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
- Output:
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. █
Common Lisp
Mapping functions
(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)))
Loop macro
Draft
Note
Program
;; 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))))))
Execution
(multiple-value-bind (scores total) (calcule-attributs-personnage) (list scores total))
- Output:
((16 9 15 12 14 14) 80)
cyril nocton (cyril.nocton@gmail.com) w/ google translate
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();
- Output:
$ ./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
Crystal
def roll_stat
dices = Array(Int32).new(4) { rand(1..6) }
dices.sum - dices.min
end
def roll_character
loop do
stats = Array(Int32).new(6) { roll_stat }
return stats if stats.sum >= 75 && stats.count(&.>=(15)) >= 2
end
end
10.times do
stats = roll_character
puts "stats: #{stats}, sum is #{stats.sum}"
end
sample output:
stats: [14, 11, 18, 14, 12, 16], sum is 85 stats: [10, 12, 13, 16, 17, 16], sum is 84 stats: [12, 17, 13, 11, 17, 13], sum is 83 stats: [16, 12, 11, 9, 16, 12], sum is 76 stats: [14, 17, 12, 15, 16, 14], sum is 88 stats: [9, 17, 17, 7, 9, 16], sum is 75 stats: [17, 14, 17, 12, 12, 13], sum is 85 stats: [16, 8, 14, 12, 11, 16], sum is 77 stats: [17, 13, 11, 10, 14, 16], sum is 81 stats: [11, 16, 11, 13, 15, 16], sum is 82
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.
- Output:
Attribs: 8, 15, 15, 16, 14, 10, sum=78, (good sum, high vals=3) - success
Dyalect
func getThree(n) {
var g3 = []
for i in 0..33 {
g3.Add(rnd(max: n) + 1)
}
g3.Sort()
g3.RemoveAt(0)
g3
}
func getSix() {
var g6 = []
for i in 0..5 {
g6.Add(getThree(6).Sum())
}
g6
}
func Array.Sum() {
var acc = 0
for x in this {
acc += x
}
acc
}
func Array.FindAll(pred) {
for x in this when pred(x) {
yield x
}
}
var good = false
while !good {
var gs = getSix()
var gss = gs.Sum()
var hvc = gs.FindAll(x => x > 14).Length()
print("attribs: \(gs), sum=\(gss), ", terminator: "")
let gl = gss >= 75 ? "good" : "low"
print("(\(gl) sum, high vals=\(hvc))", terminator: "")
good = gs.Sum() >= 75 && hvc > 1
print(" - " + (good ? "success" : "failure"))
}
- Output:
attribs: [103, 133, 110, 130, 120, 101], sum=697, (good sum, high vals=6) - success
EasyLang
func rollstat .
for i to 4
h = random 6
s += h
min = lower min h
.
return s - min
.
state$[] = [ "STR" "CON" "DEX" "INT" "WIS" "CHA" ]
len stat[] 6
repeat
sum = 0
n15 = 0
for i to 6
stat[i] = rollstat
sum += stat[i]
if stat[i] >= 15
n15 += 1
.
.
until sum >= 75 and n15 >= 2
.
for i to 6
print state$[i] & ": " & stat[i]
.
print "-------"
print "TOT: " & sum
- Output:
STR: 13 CON: 11 DEX: 16 INT: 8 WIS: 17 CHA: 18 ------- TOT: 83
Factor
USING: combinators.short-circuit dice formatting io kernel math
math.statistics qw sequences ;
IN: rosetta-code.rpg-attributes-generator
CONSTANT: stat-names qw{ Str Dex Con Int Wis Cha }
: attribute ( -- n )
4 [ ROLL: 1d6 ] replicate 3 <iota> kth-largests sum ;
: stats ( -- seq ) 6 [ attribute ] replicate ;
: valid-stats? ( seq -- ? )
{ [ [ 15 >= ] count 2 >= ] [ sum 75 >= ] } 1&& ;
: generate-valid-stats ( -- seq )
stats [ dup valid-stats? ] [ drop stats ] until ;
: stats-info ( seq -- )
[ sum ] [ [ 15 >= ] count ] bi
"Total: %d\n# of attributes >= 15: %d\n" printf ;
: main ( -- )
generate-valid-stats dup stat-names swap
[ "%s: %d\n" printf ] 2each nl stats-info ;
MAIN: main
- Output:
Str: 9 Dex: 13 Con: 14 Int: 17 Wis: 17 Cha: 11 Total: 81 # of attributes >= 15: 2
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
- Output:
STR: 14CON: 11 DEX: 15 INT: 14 WIS: 13 CHA: 15 -------- TOT: 82
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)
- Output:
*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
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 !
- Output:
roll str:13 dex:15 con:14 int:8 wis:17 cha:10 (total:77) ok
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
- Output:
Strength 17 Constitution 11 Dexterity 18 Intelligence 12 Wisdom 19 Charisma 11 ------------ Total: 88
Go
package main
import (
"fmt"
"math/rand"
"sort"
"time"
)
func main() {
s := rand.NewSource(time.Now().UnixNano())
r := rand.New(s)
for {
var values [6]int
vsum := 0
for i := range values {
var numbers [4]int
for j := range numbers {
numbers[j] = 1 + r.Intn(6)
}
sort.Ints(numbers[:])
nsum := 0
for _, n := range numbers[1:] {
nsum += n
}
values[i] = nsum
vsum += values[i]
}
if vsum < 75 {
continue
}
vcount := 0
for _, v := range values {
if v >= 15 {
vcount++
}
}
if vcount < 2 {
continue
}
fmt.Println("The 6 random numbers generated are:")
fmt.Println(values)
fmt.Println("\nTheir sum is", vsum, "and", vcount, "of them are >= 15")
break
}
}
- Output:
Sample run:
The 6 random numbers generated are: [16 15 7 14 9 15] Their sum is 76 and 3 of them are >= 15
Haskell
import Control.Monad (replicateM)
import System.Random (randomRIO)
import Data.Bool (bool)
import Data.List (sort)
character :: IO [Int]
character =
discardUntil
(((&&) . (75 <) . sum) <*> ((2 <=) . length . filter (15 <=)))
(replicateM 6 $ sum . tail . sort <$> replicateM 4 (randomRIO (1, 6 :: Int)))
discardUntil :: ([Int] -> Bool) -> IO [Int] -> IO [Int]
discardUntil p throw = go
where
go = throw >>= (<*>) (bool go . return) p
-------------------------- TEST ---------------------------
main :: IO ()
main = replicateM 10 character >>= mapM_ (print . (sum >>= (,)))
- Output:
Sample computation: (86,[15,13,17,17,13,11]) (80,[16,11,15,13,11,14]) (77,[15,8,15,11,15,13]) (76,[12,11,17,11,7,18]) (76,[11,16,8,15,15,11]) (87,[14,15,12,16,15,15]) (84,[11,11,16,15,15,16]) (77,[14,13,15,8,11,16]) (80,[12,15,11,17,15,10]) (89,[15,12,16,17,12,17])
J
'twould be more efficient to work with index origin 0, then increment the roll once at output.
roll=: 1 + 4 6 ?@:$ 6:
massage=: +/ - <./
generate_attributes=: massage@:roll
accept=: (75 <: +/) *. (2 <: [: +/ 15&<:)
Until=: conjunction def 'u^:(0-:v)^:_'
NB. show displays discarded attribute sets
NB. and since roll ignores arguments, echo would suffice in place of show
show=: [ echo
NB. use: generate_character 'name'
generate_character=: (; (+/ ; ])@:([: generate_attributes@:show Until accept 0:))&>@:boxopen
generate_character 'MrKent' 0 15 12 16 9 12 8 13 17 11 12 14 9 16 7 14 12 13 14 13 8 6 12 7 9 12 12 17 14 13 13 ┌──────┬──┬─────────────────┐ │MrKent│79│13 16 16 10 10 14│ └──────┴──┴─────────────────┘ generate_character ;: 'Ti StMary Judas' 0 12 11 12 15 13 12 13 13 14 16 8 11 0 16 14 9 11 12 12 10 14 9 11 7 9 0 ┌──────┬──┬─────────────────┐ │Ti │81│17 9 11 16 13 15 │ ├──────┼──┼─────────────────┤ │StMary│79│15 18 16 13 11 6 │ ├──────┼──┼─────────────────┤ │Judas │83│16 11 12 15 17 12│ └──────┴──┴─────────────────┘
Java
import java.util.List;
import java.util.Random;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
public class Rpg {
private static final Random random = new Random();
public static int genAttribute() {
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 void main(String[] args) {
while (true) {
List<Integer> stats =
Stream.generate(Rpg::genAttribute) // Generate some stats
.limit(6) // Take 6
.collect(toList()); // Save them in an array
int sum = stats.stream().mapToInt(Integer::intValue).sum();
long count = stats.stream().filter(v -> v >= 15).count();
if (count >= 2 && sum >= 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);
return;
}
}
}
}
- Output:
The 6 random numbers generated are: [10, 18, 9, 15, 14, 14] Their sum is 80 and 2 of them are >= 15
JavaScript
Imperative
function roll() {
const stats = {
total: 0,
rolls: []
}
let count = 0;
for(let i=0;i<=5;i++) {
let d6s = [];
for(let j=0;j<=3;j++) {
d6s.push(Math.ceil(Math.random() * 6))
}
d6s.sort().splice(0, 1);
rollTotal = d6s.reduce((a, b) => a+b, 0);
stats.rolls.push(rollTotal);
stats.total += rollTotal;
}
return stats;
}
let rolledCharacter = roll();
while(rolledCharacter.total < 75 || rolledCharacter.rolls.filter(a => a >= 15).length < 2){
rolledCharacter = roll();
}
console.log(`The 6 random numbers generated are:
${rolledCharacter.rolls.join(', ')}
Their sum is ${rolledCharacter.total} and ${rolledCharacter.rolls.filter(a => a >= 15).length} of them are >= 15`);
- Output:
Sample run:
The 6 random numbers generated are: 11, 17, 12, 12, 9, 16 Their sum is 77 and 2 of them are >= 15
Functional
(Functional composition version)
(() => {
'use strict';
// main :: IO ()
const main = () =>
// 10 random heroes drawn from
// a non-finite series.
unlines(map(
xs => show(sum(xs)) +
' -> [' + show(xs) + ']',
take(10, heroes(
seventyFivePlusWithTwo15s
))
));
// seventyFivePlusWithTwo15s :: [Int] -> Bool
const seventyFivePlusWithTwo15s = xs =>
// Total score over 75,
// with two or more qualities scoring 15.
75 < sum(xs) && 1 < length(filter(
x => 15 === x, xs
));
// heroes :: Gen IO [(Int, Int, Int, Int, Int, Int)]
function* heroes(p) {
// Non-finite list of heroes matching
// the requirements of predicate p.
while (true) {
yield hero(p)
}
}
// hero :: (Int -> Bool) -> IO (Int, Int, Int, Int, Int, Int)
const hero = p =>
// A random character matching the
// requirements of predicate p.
until(p, character, []);
// character :: () -> IO [Int]
const character = () =>
// A random character with six
// integral attributes.
map(() => sum(tail(sort(map(
randomRInt(1, 6),
enumFromTo(1, 4)
)))),
enumFromTo(1, 6)
);
// GENERIC FUNCTIONS ----------------------------------
// enumFromTo :: (Int, Int) -> [Int]
const enumFromTo = (m, n) =>
Array.from({
length: 1 + n - m
}, (_, i) => m + i);
// filter :: (a -> Bool) -> [a] -> [a]
const filter = (f, xs) => xs.filter(f);
// Returns Infinity over objects without finite length.
// This enables zip and zipWith to choose the shorter
// argument when one is non-finite, like cycle, repeat etc
// length :: [a] -> Int
const length = xs =>
(Array.isArray(xs) || 'string' === typeof xs) ? (
xs.length
) : Infinity;
// map :: (a -> b) -> [a] -> [b]
const map = (f, xs) =>
(Array.isArray(xs) ? (
xs
) : xs.split('')).map(f);
// e.g. map(randomRInt(1, 10), enumFromTo(1, 20))
// randomRInt :: Int -> Int -> IO () -> Int
const randomRInt = (low, high) => () =>
low + Math.floor(
(Math.random() * ((high - low) + 1))
);
// show :: a -> String
const show = x => x.toString()
// sort :: Ord a => [a] -> [a]
const sort = xs => xs.slice()
.sort((a, b) => a < b ? -1 : (a > b ? 1 : 0));
// sum :: [Num] -> Num
const sum = xs => xs.reduce((a, x) => a + x, 0);
// tail :: [a] -> [a]
const tail = xs => 0 < xs.length ? xs.slice(1) : [];
// take :: Int -> [a] -> [a]
// take :: Int -> String -> String
const take = (n, xs) =>
'GeneratorFunction' !== xs.constructor.constructor.name ? (
xs.slice(0, n)
) : [].concat.apply([], Array.from({
length: n
}, () => {
const x = xs.next();
return x.done ? [] : [x.value];
}));
// unlines :: [String] -> String
const unlines = xs => xs.join('\n');
// until :: (a -> Bool) -> (a -> a) -> a -> a
const until = (p, f, x) => {
let v = x;
while (!p(v)) v = f(v);
return v;
};
// MAIN ---
return main();
})();
- Output:
A sample of 10 character attribute sets:
79 -> [12,15,12,15,13,12] 92 -> [17,16,15,15,17,12] 82 -> [12,14,13,13,15,15] 84 -> [15,16,18,10,15,10] 83 -> [15,12,17,14,10,15] 83 -> [14,15,10,15,14,15] 77 -> [12,13,15,11,15,11] 81 -> [15,8,16,15,15,12] 79 -> [15,15,11,17,12,9] 76 -> [14,12,9,15,15,11]
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:
< /dev/random tr -cd '0-9' | fold -w 1 | jq -Mcnr -f rgp-attributes.jq
where rgp-attributes.jq is a file containing the jq program shown below.
The jq program
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
- Output:
Example of a run which only one round of throwing the four dice
[[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.
Example of a run requiring more than one round of throwing the four dice
[[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
Julia
roll_skip_lowest(dice, sides) = (r = rand(collect(1:sides), dice); sum(r) - minimum(r))
function rollRPGtoon()
attributes = zeros(Int, 6)
attsum = 0
gte15 = 0
while attsum < 75 || gte15 < 2
for i in 1:6
attributes[i] = roll_skip_lowest(4, 6)
end
attsum = sum(attributes)
gte15 = mapreduce(x -> x >= 15, +, attributes)
end
println("New RPG character roll: $attributes. Sum is $attsum, and $gte15 are >= 15.")
end
rollRPGtoon()
rollRPGtoon()
rollRPGtoon()
- Output:
New RPG character roll: [15, 16, 15, 11, 9, 15]. Sum is 81, and 4 are >= 15. New RPG character roll: [12, 14, 15, 12, 10, 16]. Sum is 79, and 2 are >= 15. New RPG character roll: [10, 12, 13, 13, 15, 17]. Sum is 80, and 2 are >= 15.
Kotlin
import kotlin.random.Random
fun main() {
while (true) {
val values = List(6) {
val rolls = generateSequence { 1 + Random.nextInt(6) }.take(4)
rolls.sorted().take(3).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("Their sum is $vsum and $vcount of them are >= 15")
break
}
}
- Output:
Sample run:
The 6 random numbers generated are: [13, 14, 13, 15, 17, 8] Their sum is 80 and 2 of them are >= 15
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}"
- Output:
strength: 11 dexterity: 14constitution: 14 intelligence: 15
wisdom: 15 charisma: 12Attribute value total: 81
Attribule count >= 15: 2
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
- Output:
Failed... Failed... Failed... Failed... 11 15 12 16 15 15 Sum: 84 >14: 4
Mathematica / Wolfram Language
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}
- Output:
{78, {13, 15, 9, 13, 12, 16}}
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!
- Output:
(11 17 12 16 9 12) Total: 77
MiniScript
roll = function()
results = []
for i in range(0,3)
results.push ceil(rnd * 6)
end for
results.sort
results.remove 0
return results.sum
end function
while true
attributes = []
gt15 = 0 // (how many attributes > 15)
for i in range(0,5)
attributes.push roll
if attributes[i] > 15 then gt15 = gt15 + 1
end for
print "Attribute values: " + attributes.join(", ")
print "Attributes total: " + attributes.sum
if attributes.sum >= 75 and gt15 >= 2 then break
print "Attributes failed, rerolling"
print
end while
print "Success!"
- Output:
Attribute values: 11, 13, 8, 10, 8, 10 Attributes total: 60 Attributes failed, rerolling Attribute values: 11, 13, 14, 13, 15, 14 Attributes total: 80 Attributes failed, rerolling Attribute values: 13, 11, 12, 9, 13, 12 Attributes total: 70 Attributes failed, rerolling Attribute values: 18, 17, 12, 10, 17, 12 Attributes total: 86 Success!
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
Sample output:
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
OCaml
Original version by User:Vanyamil
(* 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
- Output:
11, 15, 11, 15, 14, 14,
Pascal
program attributes;
var
total, roll,score, count: integer;
atribs : array [1..6] of integer;
begin
randomize; {Initalise the random number genertor}
repeat
count:=0;
total:=0;
for score :=1 to 6 do begin
{roll:=random(18)+1; produce a number up to 18, pretty much the same results}
for diceroll:=1 to 4 do dice[diceroll]:=random(6)+1; {roll 4 six sided die}
{find lowest rolled dice. If we roll two or more equal low rolls then we
eliminate the first of them, change '<' to '<=' to eliminate last low die}
lowroll:=7;
lowdie:=0;
for diceroll:=1 to 4 do if (dice[diceroll] < lowroll) then begin
lowroll := dice[diceroll];
lowdie := diceroll;
end;
{add up higest three dice}
roll:=0;
for diceroll:=1 to 4 do if (diceroll <> lowdie) then roll := roll + dice[diceroll];
atribs[score]:=roll;
total := total + roll;
if (roll>15) then count:=count+1;
end;
until ((total>74) and (count>1)); {this evens out different rolling methods }
{ Prettily print the attributes out }
writeln('Attributes :');
for count:=1 to 6 do
writeln(count,'.......',atribs[count]:2);
writeln(' ---');
writeln('Total ',total:3);
writeln(' ---');
end.
- Output:
Attributes : 1....... 5 2.......13 3....... 8 4.......17 5.......15 6.......18 --- Total 76 --- Attributes : 1.......17 2.......13 3.......17 4.......12 5.......12 6.......16 --- Total 87 --- Attributes : 1.......16 2....... 9 3.......10 4.......17 5.......15 6....... 9 --- Total 76
Perl
use strict;
use List::Util 'sum';
my ($min_sum, $hero_attr_min, $hero_count_min) = <75 15 3>;
my @attr_names = <Str Int Wis Dex Con Cha>;
sub heroic { scalar grep { $_ >= $hero_attr_min } @_ }
sub roll_skip_lowest {
my($dice, $sides) = @_;
sum( (sort map { 1 + int rand($sides) } 1..$dice)[1..$dice-1] );
}
my @attr;
do {
@attr = map { roll_skip_lowest(6,4) } @attr_names;
} until sum(@attr) >= $min_sum and heroic(@attr) >= $hero_count_min;
printf "%s = %2d\n", $attr_names[$_], $attr[$_] for 0..$#attr;
printf "Sum = %d, with %d attributes >= $hero_attr_min\n", sum(@attr), heroic(@attr);
- Output:
Str = 13 Int = 15 Wis = 9 Dex = 19 Con = 17 Cha = 10 Sum = 83, with 3 attributes >= 15
Phix
with javascript_semantics sequence numbers = repeat(0,6) integer t,n while true do for i=1 to length(numbers) do sequence ni = sq_rand(repeat(6,4)) numbers[i] = sum(ni)-min(ni) end for t = sum(numbers) n = sum(sq_ge(numbers,15)) if t>=75 and n>=2 then exit end if ?"re-rolling..." -- (occasionally >20) end while printf(1,"The 6 attributes generated are:\n") printf(1,"strength %d, dexterity %d, constitution %d, "& "intelligence %d, wisdom %d, and charisma %d.\n", numbers) printf(1,"\nTheir sum is %d and %d of them are >=15\n",{t,n})
- Output:
"re-rolling..." "re-rolling..." The 6 attributes generated are: strength 14, dexterity 15, constitution 17, intelligence 9, wisdom 13, and charisma 18. Their sum is 86 and 3 of them are >=15
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 ?
- Output:
STR: 14 CON: 9 DEX: 17 INT: 16 WIS: 13 CHA: 15 ------- TOT: 84 === Press any key to exit ===
PHP
Version 1
<?php
$attributesTotal = 0;
$count = 0;
while($attributesTotal < 75 || $count < 2) {
$attributes = [];
foreach(range(0, 5) as $attribute) {
$rolls = [];
foreach(range(0, 3) as $roll) {
$rolls[] = rand(1, 6);
}
sort($rolls);
array_shift($rolls);
$total = array_sum($rolls);
if($total >= 15) {
$count += 1;
}
$attributes[] = $total;
}
$attributesTotal = array_sum($attributes);
}
print_r($attributes);
Version 2
<?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";
- Output:
Attribute Total: 80 Attributes: 10, 12, 16, 17, 14, 11
Plain English
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.
- Output:
13, 10, 17, 10, 18, 14 Total: 82 Number of stats greater than fourteen: 2
PureBasic
#heroicAttributeMinimum = 15
#heroicAttributeCountMinimum = 2
#attributeSumMinimum = 75
#attributeCount = 6
Procedure roll_attribute()
Protected i, sum
Dim rolls(3)
For i = 0 To 3
rolls(i) = Random(6, 1)
Next i
;sum the highest three rolls
SortArray(rolls(), #PB_Sort_Descending)
For i = 0 To 2
sum + rolls(i)
Next
ProcedureReturn sum
EndProcedure
Procedure displayAttributes(List attributes(), sum, heroicCount)
Protected output$
output$ = "Attributes generated: ["
ForEach attributes()
output$ + attributes()
If ListIndex(attributes()) <> #attributeCount - 1: output$ + ", ": EndIf
Next
output$ + "]"
PrintN(output$)
PrintN("Total: " + sum + ", Values " + #heroicAttributeMinimum + " or above: " + heroicCount)
EndProcedure
Procedure Gen_attributes()
Protected i, attributesSum, heroicAttributesCount
NewList attributes()
Repeat
ClearList(attributes())
attributesSum = 0: heroicAttributesCount = 0
For i = 1 To #attributeCount
AddElement(attributes())
attributes() = roll_attribute()
attributesSum + attributes()
heroicAttributesCount + Bool(attributes() >= #heroicAttributeMinimum)
Next
Until attributesSum >= #attributeSumMinimum And heroicAttributesCount >= #heroicAttributeCountMinimum
displayAttributes(attributes(), attributesSum, heroicAttributesCount)
EndProcedure
If OpenConsole("RPG Attributes Generator")
Gen_attributes()
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf
Sample output:
Attributes generated: [13, 17, 17, 11, 9, 17] Total: 84, Values 15 or above: 3
Python
Python: Simple
import random
random.seed()
attributes_total = 0
count = 0
while attributes_total < 75 or count < 2:
attributes = []
for attribute in range(0, 6):
rolls = []
for roll in range(0, 4):
result = random.randint(1, 6)
rolls.append(result)
sorted_rolls = sorted(rolls)
largest_3 = sorted_rolls[1:]
rolls_total = sum(largest_3)
if rolls_total >= 15:
count += 1
attributes.append(rolls_total)
attributes_total = sum(attributes)
print(attributes_total, attributes)
- Output:
Sample run:
(74, [16, 10, 12, 9, 16, 11])
Python: Nested Comprehensions #1
import random
random.seed()
total = 0
count = 0
while total < 75 or count < 2:
attributes = [(sum(sorted([random.randint(1, 6) for roll in range(0, 4)])[1:])) for attribute in range(0, 6)]
for attribute in attributes:
if attribute >= 15:
count += 1
total = sum(attributes)
print(total, attributes)
- Output:
Sample run:
(77, [17, 8, 15, 13, 12, 12])
Python: Nested Comprehensions #2
With comprehensions for checking candidate values in the while expression.
import random
def compute():
values = []
while (sum(values) < 75 # Total must be >= 75
or sum(1 for v in values if v >= 15) < 2): # Two must be >= 15
values = [sum(sorted(random.randint(1, 6) for _ in range(4))[1:]) for _ in range(6)]
return sum(values), values
for i in range(3):
print(*compute())
- Output:
81 [12, 17, 9, 9, 17, 17] 75 [16, 7, 13, 12, 15, 12] 81 [15, 11, 15, 16, 10, 14]
Python: Functional composition
Composing a hero-generator from reusable functions:
'''RPG Attributes Generator'''
from itertools import islice
from random import randint
from operator import eq
# heroes :: Gen IO [(Int, Int, Int, Int, Int, Int)]
def heroes(p):
'''Non-finite list of heroes matching
the requirements of predicate p.
'''
while True:
yield tuple(
until(p)(character)([])
)
# character :: () -> IO [Int]
def character(_):
'''A random character with six
integral attributes.
'''
return [
sum(sorted(map(
randomRInt(1)(6),
enumFromTo(1)(4)
))[1:])
for _ in enumFromTo(1)(6)
]
# ------------------------- TEST --------------------------
# main :: IO ()
def main():
'''Test :: Sample of 10'''
# seventyFivePlusWithTwo15s :: [Int] -> Bool
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)))
)
print('A sample of 10:\n')
print(unlines(
str(sum(x)) + ' -> ' + str(x) for x
in take(10)(heroes(
seventyFivePlusIncTwo15s
))
))
# ------------------------- GENERIC -------------------------
# curry :: ((a, b) -> c) -> a -> b -> c
def curry(f):
'''A curried function derived
from an uncurried function.
'''
return lambda x: lambda y: f(x, y)
# enumFromTo :: Int -> Int -> [Int]
def enumFromTo(m):
'''Enumeration of integer values [m..n]'''
return lambda n: range(m, 1 + n)
# randomRInt :: Int -> Int -> IO () -> Int
def randomRInt(m):
'''The return value of randomRInt is itself
a function. The returned function, whenever
called, yields a a new pseudo-random integer
in the range [m..n].
'''
return lambda n: lambda _: randint(m, n)
# take :: Int -> [a] -> [a]
# take :: Int -> String -> String
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))
)
# unlines :: [String] -> String
def unlines(xs):
'''A single string formed by the intercalation
of a list of strings with the newline character.
'''
return '\n'.join(xs)
# until :: (a -> Bool) -> (a -> a) -> a -> a
def until(p):
'''The result of repeatedly applying f until p holds.
The initial seed value is x.
'''
def go(f, x):
v = x
while not p(v):
v = f(v)
return v
return lambda f: lambda x: go(f, x)
if __name__ == '__main__':
main()
A sample of 10: 76 -> (15, 14, 12, 9, 15, 11) 85 -> (12, 11, 16, 15, 15, 16) 80 -> (15, 11, 15, 9, 13, 17) 81 -> (15, 14, 12, 13, 15, 12) 82 -> (10, 12, 13, 15, 15, 17) 77 -> (9, 15, 11, 15, 15, 12) 83 -> (15, 13, 13, 15, 15, 12) 84 -> (10, 16, 15, 14, 14, 15) 79 -> (17, 15, 10, 11, 15, 11) 75 -> (15, 13, 7, 11, 14, 15)
Python: One-liner
Just because you can, doesn't mean you should.
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)])))
Attributes: [16, 15, 15, 14, 8, 10] Total: 78
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
- Output:
[ 10 16 16 8 12 15 ] Sum: 77 # of attributes > 14: 3
Racket
#lang racket
(define (d6 . _)
(+ (random 6) 1))
(define (best-3-of-4d6 . _)
(apply + (rest (sort (build-list 4 d6) <))))
(define (generate-character)
(let* ((rolls (build-list 6 best-3-of-4d6))
(total (apply + rolls)))
(if (or (< total 75) (< (length (filter (curryr >= 15) rolls)) 2))
(generate-character)
(values rolls total))))
(module+ main
(define-values (rolled-stats total) (generate-character))
(printf "Rolls:\t~a~%Total:\t~a" rolled-stats total))
- Output:
Rolls: (11 16 10 13 12 15) Total: 77
R
The base library already has an attributes function, so we avoid using that name. Otherwise, this is R's bread and butter.
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())
- Output:
STR DEX CON INT WIS CHA TOT 15 18 18 11 15 15 92
Raku
(formerly Perl 6)
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";
- Output:
(Str => 15 Int => 16 Wis => 13 Dex => 11 Con => 15 Cha => 6) Sum: 76, with 3 attributes >= 15
Red
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
- Output:
Strength: 14 Dexterity: 11 Constitution: 16 Intelligence: 9 Wisdom: 16 Charisma: 16 ----------------- Sum: 82 3 attributes > 14
REXX
version 1
/* REXX
Generates 4 random, whole values between 1 and 6.
Saves the sum of the 3 largest values.
Generates a total of 6 values this way.
Displays the total, and all 6 values once finished.
*/
Do try=1 By 1
ge15=0
sum=0
ol=''
Do i=1 To 6
rl=''
Do j=1 To 4
rl=rl (random(5)+1)
End
rl=wordsort(rl)
rsum.i=maxsum()
If rsum.i>=15 Then ge15=ge15+1
sum=sum+rsum.i
ol=ol right(rsum.i,2)
End
Say ol '->' ge15 sum
If ge15>=2 & sum>=75 Then Leave
End
Say try 'iterations'
Say ol '=>' sum
Exit
maxsum: procedure Expose rl
/**********************************************************************
* Comute the sum of the 3 largest values
**********************************************************************/
m=0
Do i=2 To 4
m=m+word(rl,i)
End
Return m
wordsort: Procedure
/**********************************************************************
* Sort the list of words supplied as argument. Return the sorted list
**********************************************************************/
Parse Arg wl
wa.=''
wa.0=0
Do While wl<>''
Parse Var wl w wl
Do i=1 To wa.0
If wa.i>w Then Leave
End
If i<=wa.0 Then Do
Do j=wa.0 To i By -1
ii=j+1
wa.ii=wa.j
End
End
wa.i=w
wa.0=wa.0+1
End
swl=''
Do i=1 To wa.0
swl=swl wa.i
End
Return strip(swl)
- Output:
I:\>rexx cast 13 13 8 15 14 11 -> 1 74 10 9 13 7 15 9 -> 1 63 15 15 14 13 17 14 -> 3 88 3 iterations 15 15 14 13 17 14 => 88
version 2
This REXX version doesn't need a sort to compute the sum of the largest three (of four) values.
/*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).*/
do 6; $= 0 /*6 values (meet criteria); attrib. sum*/
do d=1 for 4; @.d= random(1, 6) /*roll four random dice (six sided die)*/
$= $ + @.d /*also obtain their sum (of die pips).*/
end /*d*/ /* [↓] use of MIN BIF avoids sorting.*/
$= $ - min(@.1, @.2, @.3, @.4) /*obtain the sum of the highest 3 rolls*/
list= list $; $$= $$ + $ /*append $──►list; add $ to overall $$.*/
$$= $$ + $ /*add the $ sum to the overall sum. */
m= m + ($>=15) /*get # of rolls that meet the minimum.*/
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."
- output when using the default (internal) inputs:
The total for 14 12 15 16 14 15 is ──► 86, 3 entries are ≥ 15.
version 3
A variation of version 2
/*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=''
tot=0
m=0
Do 6
sum=0
Do d=1 To 4;
cast.d=random(1,6)
sum=sum+cast.d
End
min=min(cast.1,cast.2,cast.3,cast.4)
sum=sum-min
slist=slist sum
tot=tot+sum
m=m+(sum>=15)
end
Say 'the total for' space(slist) 'is -->' tot', 'm' entries are >= 15.'
end
Say 'Solution found with' n 'iterations'
- Output:
I:\>rexx rpg the total for 12 14 14 13 12 9 is --> 74, 0 entries are >= 15. the total for 15 11 13 14 10 10 is --> 73, 1 entries are >= 15. the total for 18 12 12 11 16 10 is --> 79, 2 entries are >= 15. Solution found with 3 iterations
Ring
# Project : RPG Attributes Generator
load "stdlib.ring"
attributestotal = 0
count = 0
while attributestotal < 75 or count < 2
attributes = []
for attribute = 0 to 6
rolls = []
largest3 = []
for roll = 0 to 4
result = random(5)+1
add(rolls,result)
next
sortedrolls = sort(rolls)
sortedrolls = reverse(sortedrolls)
for n = 1 to 3
add(largest3,sortedrolls[n])
next
rollstotal = sum(largest3)
if rollstotal >= 15
count = count + 1
ok
add(attributes,rollstotal)
next
attributestotal = sum(attributes)
end
showline()
func sum(aList)
num = 0
for n = 1 to len(aList)
num = num + aList[n]
next
return num
func showline()
line = "(" + attributestotal + ", ["
for n = 1 to len(attributes)
line = line + attributes[n] + ", "
next
line = left(line,len(line)-2)
line = line + "])"
see line + nl
Output:
(95, [14, 11, 14, 13, 16, 11, 16])
Ruby
res = []
until res.sum >= 75 && res.count{|n| n >= 15} >= 2 do
res = Array.new(6) do
a = Array.new(4){rand(1..6)}
a.sum - a.min
end
end
p res
puts "sum: #{res.sum}"
- Output:
[12, 14, 17, 12, 16, 9] sum: 80
Run BASIC
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
Rust
Repeats until the attributes generated meet specifications.
use rand::distributions::Uniform;
use rand::prelude::{thread_rng, ThreadRng};
use rand::Rng;
fn main() {
for _ in 0..=10 {
attributes_engine();
}
}
#[derive(Copy, Clone, Debug)]
pub struct Dice {
amount: i32,
range: Uniform<i32>,
rng: ThreadRng,
}
impl Dice {
// Modeled after d20 polyhederal dice use and notation.
// roll_pool() - returns Vec<i32> with length of vector determined by dice amount.
// attribute_out() - returns i32, by sorting a dice pool of 4d6, dropping the lowest integer, and summing all elements.
pub fn new(amount: i32, size: i32) -> Self {
Self {
amount,
range: Uniform::new(1, size + 1),
rng: thread_rng(),
}
}
fn roll_pool(mut self) -> Vec<i32> {
(0..self.amount)
.map(|_| self.rng.sample(self.range))
.collect()
}
fn attribute_out(&self) -> i32 {
// Sort dice pool lowest to high and drain all results to exclude the lowest before summing.
let mut attribute_array: Vec<i32> = self.roll_pool();
attribute_array.sort();
attribute_array.drain(1..=3).sum()
}
}
fn attributes_finalizer() -> (Vec<i32>, i32, bool) {
let die: Dice = Dice::new(4, 6);
let mut attributes: Vec<i32> = Vec::new();
for _ in 0..6 {
attributes.push(die.attribute_out())
}
let attributes_total: i32 = attributes.iter().sum();
let numerical_condition: bool = attributes
.iter()
.filter(|attribute| **attribute >= 15)
.count()
>= 2;
(attributes, attributes_total, numerical_condition)
}
fn attributes_engine() {
loop {
let (attributes, attributes_total, numerical_condition) = attributes_finalizer();
if (attributes_total >= 75) && (numerical_condition) {
println!(
"{:?} | sum: {:?}",
attributes, attributes_total
);
break;
} else {
continue;
}
}
}
- Output:
Sample output, running the generator ten times:
[15, 12, 15, 11, 10, 18] | sum: 81 [12, 14, 16, 14, 8, 17] | sum: 81 [15, 15, 11, 10, 12, 17] | sum: 80 [7, 14, 12, 17, 15, 12] | sum: 77 [13, 15, 16, 7, 11, 15] | sum: 77 [11, 13, 12, 15, 15, 12] | sum: 78 [15, 16, 13, 14, 11, 8] | sum: 77 [14, 12, 17, 16, 16, 14] | sum: 89 [11, 16, 12, 9, 16, 17] | sum: 81 [10, 18, 9, 13, 12, 16] | sum: 78 [15, 15, 14, 17, 12, 10] | sum: 83
Scala
import scala.util.Random
Random.setSeed(1)
def rollDice():Int = {
val v4 = Stream.continually(Random.nextInt(6)+1).take(4)
v4.sum - v4.min
}
def getAttributes():Seq[Int] = Stream.continually(rollDice()).take(6)
def getCharacter():Seq[Int] = {
val attrs = getAttributes()
println("generated => " + attrs.mkString("[",",", "]"))
(attrs.sum, attrs.filter(_>15).size) match {
case (a, b) if (a < 75 || b < 2) => getCharacter
case _ => attrs
}
}
println("picked => " + getCharacter.mkString("[", ",", "]"))
generated => [13,13,12,11,10,14] generated => [17,12,15,11,9,6] generated => [16,9,9,11,13,14] generated => [11,12,10,18,7,17] picked => [11,12,10,18,7,17]
Seed7
$ include "seed7_05.s7i";
const proc: main is func
local
var integer: count is 0;
var integer: total is 0;
var integer: attribIdx is 0;
var integer: diceroll is 0;
var integer: sumOfRolls is 0;
var array integer: attribute is 6 times 0;
var array integer: dice is 4 times 0;
begin
repeat
count := 0;
total := 0;
for key attribIdx range attribute do
for key diceroll range dice do
dice[diceroll] := rand(1, 6);
end for;
dice := sort(dice);
sumOfRolls := 0;
for diceroll range 2 to maxIdx(dice) do # Discard the lowest roll
sumOfRolls +:= dice[diceroll];
end for;
attribute[attribIdx] := sumOfRolls;
total +:= sumOfRolls;
if sumOfRolls >= 15 then
incr(count);
end if;
end for;
until total >= 75 and count >= 2;
writeln("Attributes:");
for key attribIdx range attribute do
writeln(attribIdx <& " ..... " <& attribute[attribIdx] lpad 2);
end for;
writeln(" ----");
writeln("Total " <& total lpad 3);
end func;
- Output:
Attributes: 1 ..... 11 2 ..... 10 3 ..... 7 4 ..... 18 5 ..... 16 6 ..... 14 ---- Total 76
True BASIC
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
- Output:
Igual que la entrada de FreeBASIC.
uBasic/4tH
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@)))
- Output:
STR: 17 CON: 15 DEX: 8 INT: 12 WIS: 14 CHA: 16 ---- TOT: 82 0 OK, 0:384
Uiua
Attr ← (/+↘1⊏⍏.⌈×6∵⋅⚂⊚4)
Attrs ← (∵⋅Attr⊚6)
Valid ← ×⊃(>1/+>14|>75/+)
⍢◌(¬Valid.Attrs)
- Output:
[14 10 16 13 17 12]
UNIX Shell
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 "$@"
- Output:
str: 12 dex: 15 con: 15 int: 14 wis: 8 cha: 12
Visual Basic .NET
repeats until a successful outcome occurs
Module Module1
Dim r As New Random
Function getThree(n As Integer) As List(Of Integer)
getThree = New List(Of Integer)
For i As Integer = 1 To 4 : getThree.Add(r.Next(n) + 1) : Next
getThree.Sort() : getThree.RemoveAt(0)
End Function
Function getSix() As List(Of Integer)
getSix = New List(Of Integer)
For i As Integer = 1 To 6 : getSix.Add(getThree(6).Sum) : Next
End Function
Sub Main(args As String())
Dim good As Boolean = False : Do
Dim gs As List(Of Integer) = getSix(), gss As Integer = gs.Sum,
hvc As Integer = gs.FindAll(Function(x) x > 14).Count
Console.Write("attribs: {0}, sum={1}, ({2} sum, high vals={3})",
String.Join(", ", gs), gss, If(gss >= 75, "good", "low"), hvc)
good = gs.Sum >= 75 AndAlso hvc > 1
Console.WriteLine(" - {0}", If(good, "success", "failure"))
Loop Until good
End Sub
End Module
- Output:
sample outputs:
attribs: 8, 15, 10, 13, 12, 8, sum=66, (low sum, high vals=1) - failure attribs: 9, 11, 7, 10, 17, 12, sum=66, (low sum, high vals=1) - failure attribs: 18, 14, 12, 11, 16, 9, sum=80, (good sum, high vals=2) - success
attribs: 10, 12, 9, 13, 17, 6, sum=67, (low sum, high vals=1) - failure attribs: 14, 11, 17, 12, 8, 11, sum=73, (low sum, high vals=1) - failure attribs: 13, 9, 12, 14, 10, 13, sum=71, (low sum, high vals=0) - failure attribs: 13, 9, 14, 14, 14, 12, sum=76, (good sum, high vals=0) - failure attribs: 11, 12, 7, 8, 10, 11, sum=59, (low sum, high vals=0) - failure attribs: 15, 4, 9, 18, 9, 12, sum=67, (low sum, high vals=2) - failure 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
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
}
}
}
- Output:
Sample run:
The six values are: [15, 16, 10, 12, 13, 13] Their total is: 79
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);
]
- Output:
Total: 79 13 17 11 10 16 12
Yabasic
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
- Output:
Igual que la entrada de FreeBASIC.
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", .{});
}
- Output:
Total: 79 Stats: [ 14 13 5 16 15 16 ]
zkl
reg attrs=List(), S,N;
do{
attrs.clear();
do(6){
abcd:=(4).pump(List,(0).random.fp(1,7)); // list of 4 [1..6] randoms
attrs.append(abcd.sum(0) - (0).min(abcd)); // sum and substract min
}
}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));
- Output:
Random numbers: 15,15,7,17,10,13 Sums to 77 with 3 >= 15
- Programming Tasks
- Solutions by Programming Task
- Simple
- 11l
- 8086 Assembly
- AArch64 Assembly
- Action!
- ALGOL 68
- APL
- ARM Assembly
- Arturo
- Atari BASIC
- BASIC256
- BQN
- C
- C sharp
- C++
- Caché ObjectScript
- CLU
- Commodore BASIC
- Common Lisp
- Cowgol
- Crystal
- Delphi
- System.SysUtils
- System.Generics.Collections
- Dyalect
- EasyLang
- Factor
- FreeBASIC
- FOCAL
- Forth
- Random.fs
- FutureBasic
- Go
- Haskell
- J
- Java
- JavaScript
- Jq
- Julia
- Kotlin
- Ksh
- LDPL
- Mathematica
- Wolfram Language
- Min
- MiniScript
- Nim
- OCaml
- Pascal
- Perl
- Phix
- Phixmonti
- PHP
- Plain English
- PureBasic
- Python
- Quackery
- Racket
- R
- Raku
- Red
- REXX
- Ring
- Ruby
- Run BASIC
- Rust
- Rand
- Scala
- Seed7
- True BASIC
- UBasic/4tH
- Uiua
- UNIX Shell
- Visual Basic .NET
- Wren
- Wren-sort
- XPL0
- Yabasic
- Zig
- Zkl