Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Random Generator

I'm testing out code to see if once the player enters this passage I can send the player to a random passage without their choosing. I'm using Harlowe and below is the code I'm trying to use. My problem is that the only passage the code chooses is the Random4 passage. I want their to be a equal chance (in this case 25%) for each of the four passages to be selected.

(set: $random to (random: 1,4))
(if: $random = 1)[(goto:"Random1")]
(if: $random = 2)[(goto:"Random2")]
(if: $random = 3)[(goto:"Random3")]
(if: $random = 4)[(goto:"Random4")]

Comments

  • I found the answer to my own question in a different discussion. Here is the quote to that answered my question.
    greyelf wrote:
    You made one small mistake in your code, you use a (single) equal sign in your (if:) macro. In Javascript (which is what Harlowe is based on) a single equals sign = represents an assignment and a double (or triple) equals sign == (or ===) is used for comparison.

    Because it is a common mistake (that even professional programmers make) to use the wrong number of equals signs (eg. to do an assignment when you meant to do a comparison) the developer of Harlowe recommends using the to and is operators instead.

    So your code should be:

    (set: $test to (random: 1,2))
    (if: $test is 1)[ You roll a 1]
    (else:)You roll a 2]


    note: The examples included in the documentation of the (set:) and (if:) macros shows the usage of the to and is operators.

    This is the code that now effectively works in my passage.
    (set: $random to (random: 1,4))
    (if: $random is 1)[(goto:"Random1")]
    (if: $random is 2)[(goto:"Random2")]
    (if: $random is 3)[(goto:"Random3")]
    (if: $random is 4)[(goto:"Random4")]
    
  • @kyle9076
    Because you can't mark one of your own comments as an Answer, could you mark this comment instead so that this thread no longer appears within the Unanswered category.
  • edited June 2016
    Many thanks for your post!

    What if I want the player to choose a random passage more than on time on my game?

    So, the next time the player chooses a random passage, I want to remove from the pool of passages that one which was previously selected.

    Is that possible?
  • edited June 2016
    If I understand you question correctly you want to have a predetermined list of passage names, you want to randomly select a passage name from the list, and you want that selected passage name to be removed from the list.

    To do this you would need to:

    1. Initialise the list of passage names.
    This must be done before the first random selection. I would suggest doing the initialisation within your story's startup tagged passage.
    (set: $passageList to (array: "Room 1", "Room 2", "Room 3", "Room 4"))
    

    2. Randomly select a passage name from list and go to that passage.
    (set: $length to $passageList's length)
    (if: $length > 0)[
    	(set: $index to (random: 1, $length))
    	(set: $passage to $passageList's ($index))
    	(set: $passageList to it - (array: $passage))
    	(go-to: $passage)
    ]
    
    ... the above code does the following:
    a. Checks that there are still passage names within the list.
    b. Random determine the index of list item that will be used.
    c. Get the name of the passage from the list
    d. Remove that passage name from the list.
    e. Go to the passage.

    3. Use the code in point 2 each time you want to randomly go to a passage from the list.

    4. You can either add new items to the list or replace the list with a different one at any time.
    ''Add passage names to the existing list''
    (set: $passageList to it + (array: "Room 5", "Room 6"))
    
    ''Replace existing list with a different one''
    (set: $passageList to (array: "Room A", "Room B", "Room C", "Room D"))
    
Sign In or Register to comment.