Page 1 of 1

[SOLVED] Capping a random integer

Posted: Wed May 08, 2019 1:27 pm
by Chiligoat
Hi everyone! Back at it again with another randint question. I have a 1-12 outcome, with potential buff (or subtraction) that could potentially pull the result up to 13. I'd like that to be scaled back down to a 12 and I thought the following would help, but instead it just sets it permanently to 12 and I just don't see why.

Code: Select all

    $ vimroll=(renpy.random.randint(1,12)+vim)
    if [vimroll]>12:
        $ vimroll=12
Maybe there's a better way?

Re: Capping a random integer

Posted: Wed May 08, 2019 2:40 pm
by nature1996
firs thing first, no need for [] to read a variable in an if clause (might actually be the cause of your problem). Other than that, I haven't seen a good way yet. I one created a python container with the add and remove function to render the calulation automatic. It might be a good choice in your case:

Code: Select all

init python in vim:
	vim = 0
	def set(val):
		global vim
		vim = val
	
	def get():
		return vim
		
	def roll():
		global vim
		val = renpy.random.randint(1,12) +vim
		if val > 12:
			val = 12
		return val
		
Then in you game you should be able to get a direct value with vim.roll()

Edit: if you have a lot of characteristic that act like this, you migth want to create a class instead. You could also do the same, but for the different dice instead.

Re: Capping a random integer

Posted: Wed May 08, 2019 3:48 pm
by Remix
or...

$ vimroll = min( 12, (renpy.random.randint(1,12)+vim) )

Re: Capping a random integer

Posted: Thu May 09, 2019 8:09 pm
by lacticacid
Chances are, it's because of the brackets. I do this with variables for my game all the time, and it works perfectly well for me.
Change [vimroll] to vimroll, and it should be fine.

Re: Capping a random integer

Posted: Tue May 14, 2019 1:02 pm
by Chiligoat
@lacticacid, @nature1996: you were both completely right, it was my brackets that messed things up! I should've seen it, but now it's clearer, so thank you both.

@Remix: doing it this way is much smoother; the less computational time the better. Would you mind explaining what's actually going on in the equation? Why does "min" cap things when, you'd think, "max" would be the operative word? Unless that's not what min stands for at all XD

Re: Capping a random integer

Posted: Tue May 14, 2019 9:09 pm
by philat
It means take the smaller of 12 and the result of the random roll. max() does the opposite (take the larger number).

Re: Capping a random integer

Posted: Thu May 16, 2019 12:59 pm
by Chiligoat
philat wrote: Tue May 14, 2019 9:09 pm It means take the smaller of 12 and the result of the random roll. max() does the opposite (take the larger number).
Ahh, okay! Thank you for taking the time to explain; I really appreciate it!