Using Log math to make weighted random number generator

I’m attempting to create a weighted random number generator in order to determine the “level” of things to spawn into my level.

For example, I have 10 types (levels) of something, and I want level 1 things to spawn twice as frequently as level 2 things, which in turn are twice as frequent as level 3 things… etc…

So because this is a base 2 logarithmic progression (I think), I should be able to use the float from random stream variable in conjunction with the loge node somehow in the construction script… but I have NO idea how.

Can anyone help me out on this please?

*edit:

Currently working around it by setting the float as a variable and using “float in range” nodes and branches with the values manually entered into them. Very ugly and hacky, but it works.

Actually that’s a straight forward exponential function.

For example you have your tier 10 enemy which spawns x times or x2^0 (2^0 = 1 so x1 which is x). You want your tier 9 to spawn twice as often so it’s x2^1. For your tier 8 it’s x22 so x2^2. For tier 7 x2^3 so basically you have x2^y

In nodes you just take a multiplication node which takes X as input and 2 ^ y which you will have to set separately for each spawn (or write a function which takes x and y as input and provides the result). Place that in front of each random number generator and you’re good (you will need one for each spawn probability)

Hope this helps :slight_smile:

EDIT: Oh yea. The resulting number gets simply inserted into a FOR loop which actually spawns your actor (the enemy in this case)

I am but a simple blueprint script kiddie and don’t mess around with C++, but looks like that would do what I want it to do. Thanks anyway.

What about:

I’m almost certain you’re right, but I’m not wrapping me head around that one. I just can’t translate it in my tiny mind into what I’d need to do in Blueprint.

Well this could be one way to do it.

Obviously it will provide a random number between 0 and the variable which may not be ideal. You might wanna add something to limit that range or simply provide a static amount which would be simply removing the “random int”.

The result simply goes into a FOR loop node. Inside of the body you just spawn or do whatever you want to do.

Ahhhhhhhh thank you! That’s a lot simpler than I was imagining.

I’ll test it out soon. Thanks again.