RNG with set responses for a range of possible numbers

Is there a way to set up an RNG which gives a specified response for a range of RNG outputs? For example, “$(user) rolls the D20 and hits a (insert RNG output),” then based on the RNG output, gives defined response 1=‘response a’, 2-7=response “b” 8-13=‘response c’, 14-19=‘response d’, and 20=‘response e’?

Here’s such an rng.

$(user) rolls the D20 and hits a $(eval n=Math.ceil(Math.random()*20);r=n==1?`response a`:(2<=n<=7?`response b`:(8<=n<=13?`response c`:(14<=n<=19?`response d`:`response e`)));n+`. `+r)

Perfect, this is exactly what I was looking for! Thank you!

The code only seems to provide either response a or b regardless of RNG output. Is there a way to fix that?

Hi, here is a solution I made that fixes this issue. This code also enables longer responses.

$(eval z=decodeURIComponent;const a=z("$(querystring $(user))"),{die:b,responses:c}=JSON.parse(z("$(querystring $(urlfetch json https://pastebin.com/raw/3bNSGLcG))")),d=(a=>Math.ceil(Math.random()*a))(b),e=((a,b)=>{const c=Object.keys(a).map(Number).sort((c,a)=>a-c);for(const d of c)if(b>=d)return a[d];throw new Error("responses")})(c,d);`${a} rolls the D${b} and hits a ${d}. ${e}`)

Note the https://pastebin.com/raw/3bNSGLcG link in the command. Visit this link and notice the structure of the text. "die": 20 specifies that the die has 20 sides. "8": "response c" specifies that any roll between 8 and the next number (in this case 14) will produce the response “response c”.

Now copy the text on that page. Visit https://pastebin.com and paste it into the box. Make your modifications to the responses or die numbers. You must take care to preserve the existing commas and quotes. Now click “Create New Paste”. You will be redirected to a new page with your paste. Notice the 8-character identifier present at the end of your browser’s address bar. Copy this identifier and replace the 3bNSGLcG in my code with yours. Now you are ready to add the command.

1 Like

This handled what I was looking to do beautifully, thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.