[SOLVED] Capping a random integer

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
Chiligoat
Regular
Posts: 25
Joined: Thu Mar 21, 2019 7:42 pm
Projects: Witches 4 Hire
Location: Sweden
Contact:

[SOLVED] Capping a random integer

#1 Post by Chiligoat » Wed May 08, 2019 1:27 pm

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?
Last edited by Chiligoat on Thu May 16, 2019 12:59 pm, edited 1 time in total.

User avatar
nature1996
Regular
Posts: 62
Joined: Wed Jun 21, 2017 10:35 am
Contact:

Re: Capping a random integer

#2 Post by nature1996 » Wed May 08, 2019 2:40 pm

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.
Je parle aussi français

User avatar
Remix
Eileen-Class Veteran
Posts: 1375
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Capping a random integer

#3 Post by Remix » Wed May 08, 2019 3:48 pm

or...

$ vimroll = min( 12, (renpy.random.randint(1,12)+vim) )
Mad Scientist Todo List:
  • Finish Improved Event Handler
  • Implement DragonBones animation as a Creator Defined Container
  • Develop Cartoon Speech Bubble dialogue
  • Finish Bitmask collision and rebound vector system
  • Develop time based building mechanic
  • Others
  • *Find a superb artist and actually write a game*

lacticacid
Regular
Posts: 36
Joined: Fri Nov 23, 2018 6:44 pm
Contact:

Re: Capping a random integer

#4 Post by lacticacid » Thu May 09, 2019 8:09 pm

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.
~There is almost always a better, easier way to approach a problem.~

User avatar
Chiligoat
Regular
Posts: 25
Joined: Thu Mar 21, 2019 7:42 pm
Projects: Witches 4 Hire
Location: Sweden
Contact:

Re: Capping a random integer

#5 Post by Chiligoat » Tue May 14, 2019 1:02 pm

@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

philat
Eileen-Class Veteran
Posts: 1700
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Capping a random integer

#6 Post by philat » 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).

User avatar
Chiligoat
Regular
Posts: 25
Joined: Thu Mar 21, 2019 7:42 pm
Projects: Witches 4 Hire
Location: Sweden
Contact:

Re: Capping a random integer

#7 Post by Chiligoat » Thu May 16, 2019 12:59 pm

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!

Post Reply

Who is online

Users browsing this forum: Google [Bot], LateWhiteRabbit