# How to let verse randomly choose an outcome?

**URL:** <https://forums.unrealengine.com/t/how-to-let-verse-randomly-choose-an-outcome/1343490>\
**Category:** Verse\
**Tags:** unreal-editor-for-fortnite, question, fortnite\
**Created:** [October 24, 2023, 9:20pm UTC](https://forums.unrealengine.com/t/how-to-let-verse-randomly-choose-an-outcome/1343490 "2023-10-24T21:20:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![LyssaSugarNya](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/lyssasugarnya/32/722321_2.png) [@LyssaSugarNya](https://forums.unrealengine.com/u/LyssaSugarNya)\
**Post date:** [October 24, 2023, 9:20pm UTC](https://forums.unrealengine.com/t/how-to-let-verse-randomly-choose-an-outcome/1343490/1 "2023-10-24T21:20:11Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![SeedohFN](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/seedohfn/32/720959_2.png) [@SeedohFN](https://forums.unrealengine.com/u/SeedohFN)\
**Post date:** [October 24, 2023, 10:17pm UTC](https://forums.unrealengine.com/t/how-to-let-verse-randomly-choose-an-outcome/1343490/2 "2023-10-24T22:17:38Z")

</div>

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:

```auto
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

---

<div class="post-metadata">

**Author:** ![LyssaSugarNya](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/lyssasugarnya/32/722321_2.png) [@LyssaSugarNya](https://forums.unrealengine.com/u/LyssaSugarNya)\
**Post date:** [October 25, 2023, 3:02pm UTC](https://forums.unrealengine.com/t/how-to-let-verse-randomly-choose-an-outcome/1343490/3 "2023-10-25T15:02:56Z")

</div>

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](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/8/f/98ffec648c5c0dd7723911ed15be7960b5f62cfe.png)  
2nd screenshot:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/9/1/791ccd86898ea50f79b60e55e88a8c18766657b1.png)  
3rd screenshot:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/8/e/78ee2199b1bf6137c965ae46a3413395202090e4.png)  
(everytime somebody votes to AR1 or AR2, it adds +1)

4th screenshot:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/9/5/895f1f954ccb026171ba465cebd1afa394f189dc.png)

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.

---

<div class="post-metadata">

**Author:** ![SeedohFN](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/seedohfn/32/720959_2.png) [@SeedohFN](https://forums.unrealengine.com/u/SeedohFN)\
**Post date:** [October 25, 2023, 5:40pm UTC](https://forums.unrealengine.com/t/how-to-let-verse-randomly-choose-an-outcome/1343490/4 "2023-10-25T17:40:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![LyssaSugarNya](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/lyssasugarnya/32/722321_2.png) [@LyssaSugarNya](https://forums.unrealengine.com/u/LyssaSugarNya)\
**Post date:** [October 25, 2023, 6:55pm UTC](https://forums.unrealengine.com/t/how-to-let-verse-randomly-choose-an-outcome/1343490/5 "2023-10-25T18:55:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [November 24, 2023, 6:55pm UTC](https://forums.unrealengine.com/t/how-to-let-verse-randomly-choose-an-outcome/1343490/6 "2023-11-24T18:55:45Z")

</div>

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