• News

• Industries

• Learning & Support

• Community

• Marketplace # Need a solid blueprint randomization technique for selecting multiple objects

I am working on a game and I am trying to figure out a good technique for randomization, I have different items and each have a chance of appearing. I am not writing the blueprint code but am trying to figure out a good solid technique to hand off to the blueprint programmer. So far this is what I have from my design document:

For deciding randomness in the game, anything that needs to be randomized and uses rarity can use this number generator technique:

Numbers will be generated from 0 to a defined number and a randomized item will pick a defined number between a number within the defined number range. So if i have 4 random crates, a common, uncommon, rare, and ultra rare crate the common crate can have a number range from 0 to 10. And choose a number or number range between that, and since its such a small range the chance of getting a common crate is very high, for an ultra rare crate a range of 0 to 500 could be picked and a range between that would be chosen.

Randomization Blueprint

**Note: **

1. The values below should be set and used by anything that uses randomization. These values should be part of the crates blueprint since each crate will need to have its own randomization numbers, it should be under a new category called “Randomization Protocol”. The old randomization code should be deprecated.

2. If i wanted to have a single digit number instead of a range of numbers for BetweenFirstNumber and BetweenSecondNumber I would just enter that number in both of those fields.

3. NumberRangeStart[Int] - The minimum and maximum values that the random number will be chosen from(0-100 etc.)

4. NumberRangeEnd[Int]

5. **BetweenFirstNumber[Int] - the number between the first and second value where if landed on a number between these two values will choose the item. **

6. BetweenSecondNumber[Int]

I am unsure if this will work or not, at least the way i intend it to. In the game I have 4 items a common item, an uncommon item a rare item and an ultra rare item, i need a way to make sure that each item appears according to rarity, but our current system doesnt seem to work like that. can someone please help me out. Preferably with some blueprint examples or at least a good explanation. Thanks in advance.

I’ve created a function that takes in arguments of rarity, which may give you some insight. See the screenshot. The “Boundary” parameters determine rarity, in %

It operates in conjunction with an array of meshes, sorted by rarity (most common at 0, least common at the end) so my function returns an index to choose from this array range.

Step 1: Generate a random number between 1 and Max, save it to a local variable.

Step 2: Set a local variable “Accumulation” to the value of the first rarity value (in this example: 40)

Step 3: ForEach loop over your rarity array.

Step 4: Check if the random number is less than (or equal to) the value of the first rarity. (In this example: 40). If not, let this iteration fizzle out (I block the progress if the loop index is 0) and proceed to the next iteration. If yes then return the loop index.

Step 5: Add the value of the next element to the Accumulation variable (original value: 40, + next value 30 = 70) then check if the random number is less than or equal to the Accumulation value. This can only be true if the value is between 41 and 70, because we already know that the value was not less than or equal to 40.

This gives you 40% chance for the first rarity, 30% chance for the second rarity. This loop should continue to follow steps 4 and 5 until the comparison is true, in which case return the index from the loop. I wrote this to allow me to change the random range to any value, and have as many possibilities as I have assets, so I can change the max int to 10,000 and have a 0.01% to spawn an object, rather than the current max of 100 allowing a minimum of 1% chance to spawn something.

I’ve rewritten this in C++, it gives a much clearer example of the methodology than blueprints, I hope it is simple enough for you to convert it to blueprints if you haven’t used C++ with UE4. See image for usage.

``````

void MyClassOrFunctionLibrary::WeightedRandom(TArray<int> Boundaries, int &ReturnInt, int Max) {

int Random = FMath::RandRange(1, Max);    //generate random int
int Accumulation = 0;                    //

for (int i = 0; i < Boundaries.Num(); i++) {
Accumulation += Boundaries*;        //set to first element value, then add the second, then the third etc... (40,30,10... 9, 1)
if (Random <= Accumulation) {        //return when the random int is less than accumulation
ReturnInt = i;
return;
}
}
ReturnInt = -1;
return;
}

``````

Hi, thank you for your help, I appreciate it. Im unsure if my programmer knows C++, I dont think he does, but I have enough C++ experience to where I can explain this to him.