# How to get random different numbers in range

**URL:** <https://forums.unrealengine.com/t/how-to-get-random-different-numbers-in-range/310769>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [March 13, 2015, 6:17am UTC](https://forums.unrealengine.com/t/how-to-get-random-different-numbers-in-range/310769 "2015-03-13T06:17:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![DylanVelez\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DylanVelez\_1](https://forums.unrealengine.com/u/DylanVelez_1)\
**Post date:** [March 13, 2015, 6:17am UTC](https://forums.unrealengine.com/t/how-to-get-random-different-numbers-in-range/310769/1 "2015-03-13T06:17:34Z")

</div>

I’m having a little of a hard time finding the logic behind my problem, let’s say i have a range that goes from 1 to N, how could i in blueprint pick a random quantity of integer numbers between 1 and N which are all different?  
Basically I need:

Pick a random quantity (1 \<= Q \<= N ) of random integer numbers ( 1 \<= X \<= N ) without X being repeated, all in blueprints.

---

<div class="post-metadata">

**Author:** ![Allar](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Allar](https://forums.unrealengine.com/u/Allar)\
**Post date:** [March 13, 2015, 6:31am UTC](https://forums.unrealengine.com/t/how-to-get-random-different-numbers-in-range/310769/2 "2015-03-13T06:31:21Z")

</div>

You could fill an array of N size with numbers 1 through N, perform a shuffle on that array, then grab the first X elements.

---

<div class="post-metadata">

**Author:** ![ZombieKemist](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ZombieKemist](https://forums.unrealengine.com/u/ZombieKemist)\
**Post date:** [March 13, 2015, 3:54pm UTC](https://forums.unrealengine.com/t/how-to-get-random-different-numbers-in-range/310769/3 "2015-03-13T15:54:48Z")

</div>

Allar’s suggestion is excellent for smaller ranges. That is how I would go about it for small values of N. For a larger N you might not want to create an array and shuffle the entire thing (say N is 100,000 or 1,000,000). Instead, if you need a large range, I would create a loop to add the random values to an array using the Add Unique node. That should prevent duplicates from showing up.

---

<div class="post-metadata">

**Author:** ![DylanVelez\_1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DylanVelez\_1](https://forums.unrealengine.com/u/DylanVelez_1)\
**Post date:** [March 13, 2015, 9:31pm UTC](https://forums.unrealengine.com/t/how-to-get-random-different-numbers-in-range/310769/4 "2015-03-13T21:31:09Z")

</div>

Thanks, that suited my needs.

---

<div class="post-metadata">

**Author:** ![Allar](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Allar](https://forums.unrealengine.com/u/Allar)\
**Post date:** [March 13, 2015, 9:32pm UTC](https://forums.unrealengine.com/t/how-to-get-random-different-numbers-in-range/310769/5 "2015-03-13T21:32:36Z")

</div>

Aye, this is definitely a better way to go for large values. I tend to not recommend this though because people sometimes create a bad source of random numbers and end up in occasional infinite loops. On the other hand, the above solution can be sped up in large cases by allocating space in the array ahead of time with a Resize node. I think the best possible solution would be to steal from cryptographic algorithims and use a seedable one way modulus generator as those functions are guaranteed to result in a unique and even distribution within a finite range

---

<div class="post-metadata">

**Author:** ![JakobR](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@JakobR](https://forums.unrealengine.com/u/JakobR)\
**Post date:** [April 25, 2016, 12:18pm UTC](https://forums.unrealengine.com/t/how-to-get-random-different-numbers-in-range/310769/6 "2016-04-25T12:18:20Z")

</div>

How exactly would this look in blueprint? The ‘shuffle’ node does only have an exec output and the ‘GET’ node an item node. How would I get an item after it has shuffled?

---

<div class="post-metadata">

**Author:** ![AndrewFord](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/andrewford/32/755855_2.png) [@AndrewFord](https://forums.unrealengine.com/u/AndrewFord)\
**Post date:** [October 14, 2021, 5:14pm UTC](https://forums.unrealengine.com/t/how-to-get-random-different-numbers-in-range/310769/7 "2021-10-14T17:14:47Z")

</div>

Create a macro library

 ![350367-2.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/e/b/debf3c0f4ddef992a58b87976ecbbfa8900c02db.png)
