[Answered] generating random number with len(variable)

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
Xasrai
Newbie
Posts: 11
Joined: Wed Dec 14, 2016 5:27 am
Contact:

[Answered] generating random number with len(variable)

#1 Post by Xasrai »

Hello. I'm wondering if it's possible to use random to generate a number that is within the confines of the length of a list?


What I'm trying to do is take the sum of 2 lists, then randomly choose one of the numbers from one of the lists to decrement by one, until the total of both lists is no longer above 10.

Code: Select all

#This code theoretically prevents exposure  and desire from being above 10 but only by removing from exposure. 


$ i = 0
while (sum(desire_list) + sum(exposure_list)) > 10:
    $k = renpy.random.randint(0,9)
    if exposure_list[k] > 0:
	    $exposure_list[k] -=1

$ i += 1 
What I WANT is to be able to define random like this:

Code: Select all

    $k = renpy.random.randint(0,[len(exposure_list)])
so that no matter how many variables get added to the exposure list, the game will always roll within the value of the list. It's not REALLY important, as I can just update the second number to reflect the total length of list as a final part of creating the game, but if I forget, this could throw an infinite loop, if >10 were allocated to exposure_list in an index higher than 9.
Last edited by Xasrai on Tue Jan 02, 2018 10:05 am, edited 1 time in total.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2402
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: generating random number with len(variable)

#2 Post by Ocelot »

What is wrong with $k = renpy.random.randint(0, len(exposure_list) - 1) ?
< < insert Rick Cook quote here > >

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
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: generating random number with len(variable)

#3 Post by Remix »

Code: Select all

$ i = 0
while sum( desire_list + exposure_list ) > 10:
    item = renpy.random.choice( desire_list + exposure_list )
    item = max( 0, item - 1 )
$ i += 1
Theoretically should work, presuming I read what you want properly
Frameworks & Scriptlets:

Xasrai
Newbie
Posts: 11
Joined: Wed Dec 14, 2016 5:27 am
Contact:

Re: generating random number with len(variable)

#4 Post by Xasrai »

Ocelot wrote: Tue Jan 02, 2018 6:11 am What is wrong with $k = renpy.random.randint(0, len(exposure_list) - 1) ?
Absolutely nothing at all. I was apparently just screwing up with my syntax.

Thanks for your help!

Post Reply

Who is online

Users browsing this forum: Google [Bot]