How to let verse randomly choose an outcome?

Hello, I have made a voting system where players can vote for the weapons they like and the weapon with the highest votes will be granted to all players (Via weapon granter). But, when there is 2 weapons with the same number of votes, I want verse to randomly select 1 between the 2 item granters, that is going to grant players the weapon. Is there a way to do this?

I’ll assume you used arrays to define the item granters. In this case, you can use GetRandomInt(MinRange, MaxRange) to get a random value of type :int. Here is an example:

using { /Fortnite.com/Devices }
using { /Verse.org/Random }
using { /Verse.org/Simulation }

# A Verse-authored creative device that can be placed in a level
Manager := class(creative_device):
    MyItems: []item_granter_device = array{}
    
    # Runs when the device is started in a running game
    OnBegin<override>()<suspends>:void=
        # Will get a value between 0 and the amount of item granter devices in 'MyItems'
        IDX:= GetRandomInt(0, MyItems.Length-1) 
        if (Item:= MyItems[IDX]):
            Item.GrantItem(<# agent here #>)

Alternatively, you could write GetRandomInt(0, 1) to decide between the two items with same vote

2 Likes

Thanks for your reply. And no, I did not use arrays to define item granters. I’m new to verse and coding in general, so my way of coding is not very optimal but I somehow made it work for the voting system. But when votes are tied, I did try to use GetRandomInt() but for some reason I don’t know how to utilize this properly.
1st screenshot:
image
2nd screenshot:
image
3rd screenshot:
image
(everytime somebody votes to AR1 or AR2, it adds +1)

4th screenshot:
image

I tried to use GetRandomInt to generate random number between 1 and 2 and named them “Tiebreaker” In the 1st screenshot.
And then in the 4th screenshot I tried to use Tiebreaker In a “if” statement but failed… Is there a different way to do this even if I didn’t use arrays to define the item granters?

Regardless, I appreciate your response because next time I do something like this, I know that something like arrays exist.

In your case it is likely you need to do some additional work. If I were you and in this scenario I would approach it this way:

  • First check the winning weapon which has the most number of votes. Say 6 votes for example.
  • Check again for weapons with votes equal to the highest vote which is 6
  • Add those who match the highest vote in an array. Let’s say TieAR: []int = array{AR2, AR3, AR7}. If array length is 1 then you stop here since no other weapon has the same value
  • Use GetRandomInt() and set the min value to be 0 and highest to be TieAR.Length-1 (because we are looking for the index)
  • The value returned from GetRandomInt() is the index of the winning weapon between the elements in the array TieAR. Let’s call the random returned value IDX
  • Using the value of IDX, we can access the winning weapon by writing TieAR[IDX]. Let’s say the value of IDX = 1 then if you recall the array we made earlier, the value of TieAR[IDX] is AR3 because the index of it in that array is 1

There are a lot of different approaches to solve your problem and this is what I came up with. This whole thing could be avoided if you used arrays at the first place. But that’s alright. After all we are still learning! I hope I was able to explain it pretty well.

1 Like

Thank you very much. I understand it now and will use this method!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.