random.randint() and renpy.random.randint()

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
neowired
Regular
Posts: 199
Joined: Mon Dec 01, 2008 2:33 pm
Contact:

random.randint() and renpy.random.randint()

#1 Post by neowired »

I noticed that if I import random in an init python block I can use
random.randint() everywhere in the code, instead of renpy.random.randint() which I think didn't work in python blocks anyway.

Is there some specific reason why we don't do this by default? Wouldn't it be shorter and less confusing this way.
Is this a thing which was not possible in earlier versions of renpy?

Asceai
Eileen-Class Veteran
Posts: 1258
Joined: Fri Sep 21, 2007 7:13 am
Projects: a battle engine
Contact:

Re: random.randint() and renpy.random.randint()

#2 Post by Asceai »

renpy.random works in python blocks, just not init blocks.

renpy.random is the same as random, except it plays nicely with rollback- i.e. if you do something like this:
$roll = random.randint(1, 6)
"You rolled a [roll]!"
and you rollback or save/load, the number rolled could be different. Using renpy.random avoids this, ensuring the same numbers are generated each time (plus you don't need the import) This is highly useful for e.g. games with random events or stats, ensuring that you can't just bypass by wiggling your mousewheel (although I'd probably go insane in Magical Diary if I couldn't do this, so... =P)

Naturally, in an init block there is no need for rollback so go ahead and use random without worrying about it.
Last edited by Asceai on Thu Feb 27, 2014 4:14 am, edited 1 time in total.

neowired
Regular
Posts: 199
Joined: Mon Dec 01, 2008 2:33 pm
Contact:

Re: random.randint() and renpy.random.randint()

#3 Post by neowired »

Asceai wrote:renpy.random works in python blocks, just not init blocks.

renpy.random is the same as random, except it plays nicely with rollback- i.e. if you do something like this:
$roll = random.randint(1, 6)
"You rolled a [roll]!"
and you rollback or save/load, the number rolled could be different. Using renpy.random avoids this, ensuring the same numbers are generated each time (plus you don't need the import)

Naturally, in an init block there is no need for rollback so go ahead and use random without worrying about it.
So it's for rollback, thanks, that answers my question perfectly :)

User avatar
Rosstin2
Veteran
Posts: 253
Joined: Thu Jan 09, 2014 12:42 pm
Completed: King's Ascent, Kitty Love
Projects: Queen At Arms, Rex Rocket
Organization: Aqualuft Games
Contact:

Re: random.randint() and renpy.random.randint()

#4 Post by Rosstin2 »

Interesting, this is some insight into why I couldn't use Renpy.Random in my init blocks earlier.
Image

Post Reply

Who is online

Users browsing this forum: No registered users