| Bytes | Lang | Time | Link |
|---|---|---|---|
| 032 | TIBASIC TI83 Plus | 250520T151322Z | madeforl |
| 045 | Wolfram Language Mathematica | 220501T045058Z | att |
| 034 | Charcoal | 220501T004259Z | Neil |
| 116 | Python 3.8 prerelease | 220429T085113Z | solid.py |
| 085 | JavaScript ES6 | 220429T130214Z | Arnauld |
| 164 | Bash | 220429T123240Z | matteo_c |
| 049 | Burlesque | 220429T110229Z | DeathInc |
| 008 | 05AB1E | 220429T065650Z | Kevin Cr |
TI-BASIC (TI-83 Plus), 32 bytes
Input G
Input S
Input U
Input L
{0
While G≠sum(Ans
randInt(U,L,S
End
Ans
Can take a while for inputs that have only a few possible outputs, but it works
Wolfram Language (Mathematica), 49 47 45 bytes
RandomChoice@*GroupBy[Range@##2~Tuples~#,Tr]&
Input [SplitCount, LowerLimit, UpperLimit][GrandTotal].
Charcoal, 34 bytes
NθNηNζ≔⊕⁻NζεI‽ΦEXεη⁺﹪÷ιXε…⁰ηεζ⁼Σιθ
Try it online! Link is to verbose version of code. Takes input in the order GrandTotal, SplitCount, LowerLimit, UpperLimit. Explanation:
NθNηNζ
Input the grand total, split count and lower limit.
≔⊕⁻Nζε
Input the upper limit and calculate the length of the inclusive range from the lower limit to the upper limit.
I‽ΦEXεη⁺﹪÷ιXε…⁰ηεζ⁼Σιθ
Generate the Cartesian product of the split count number of inclusive ranges, filter out those whose sum isn't the desired grand total, and output one uniformly at random.
Python 3.8 (pre-release), 116 bytes
-12 bytes thanks to Jitse
Accepts input in the form of GrandTotal,SplitCount,UpperLimit,LowerLimit.
Returns a randomly picked range, as a tuple, from a list of possible ranges.
lambda g,s,u,l:r.choice([p for p in i.product(range(l,u+1),repeat=s)if sum(p)==g])
import random as r,itertools as i
JavaScript (ES6), 85 bytes
Expects (count, lower, upper)(total).
(n,a,b)=>g=(t,k=n,s=t,...o)=>k?g(t,k-1,s-=q=a-Math.random()*(~b+a)|0,...o,q):s?g(t):o
(NB: The function is likely to stack overflow on some test cases. The above test includes some extra recovery code.)
Bash, 164 bytes
echo>0
seq 1 $2|xargs -I, sh -c "rm ,;seq $4 $3|xargs -i sh -c \"<\$((,-1)) sed s/^/{}\ />>,\""
<$2 tr \ +|sed s/+$//|bc|pr -mt $2 -|grep $1$|cut -d\ -f1|shuf -n1
Burlesque, 49 bytes
r@s1Js2rzjCBf{++g2==}f{{g1j~[}al}sa-.3 0x/rn1.+si
r@s1 # Range from lowerLimit to upperLimit and save to 1
Js2 # Duplicate grandTotal and save to 2
rz # Range [0, grandTotal]
jCB # Combinations of SplitCount
f{++g2==} # Filter for sum == grantTotal
f{{g1j~[}al} # Filter for all elements in range
sa-.3 0x/rn # Generate infinite list of random numbers [0, len)
1.+si # Take 1 and select it
05AB1E, 8 bytes
ŸIãʒOQ}Ω
Inputs in the order UpperLimit,LowerLimit,SplitCount,GrandTotal.
Try it online or verify all test cases (without the }Ω).
Explanation:
Ÿ # Take the first two (implicit) inputs and push a list in the range
# [LowerLimit,UpperLimit]
Iã # Create all combinations of a size of the third input SplitCount using
# the cartesian product
ʒ # Filter this list of lists by:
O # Sum the list
Q # Check if it's equal to the fourth (implicit) input GrandTotal
}Ω # After the filter: pop and leave a random list
# (which is output implicitly as result)