# Using Log math to make weighted random number generator

**URL:** <https://forums.unrealengine.com/t/using-log-math-to-make-weighted-random-number-generator/297960>\
**Category:** World Creation\
**Tags:** random-numbers, unreal-engine, construction-script, question, Blueprint\
**Created:** [November 2, 2014, 6:07pm UTC](https://forums.unrealengine.com/t/using-log-math-to-make-weighted-random-number-generator/297960 "2014-11-02T18:07:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zumf](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Zumf](https://forums.unrealengine.com/u/Zumf)\
**Post date:** [November 2, 2014, 6:07pm UTC](https://forums.unrealengine.com/t/using-log-math-to-make-weighted-random-number-generator/297960/1 "2014-11-02T18:07:46Z")

</div>

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.

 ![19821-semi-random01.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/e/8/2e826def2e357dde09cda68d77a6615f79f8d590.png)

 ![19822-semi-random02.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/3/c/93ce8993b178b243fc18c66e144eb042e8078495.png)

---

<div class="post-metadata">

**Author:** ![Erasio](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Erasio](https://forums.unrealengine.com/u/Erasio)\
**Post date:** [November 2, 2014, 6:38pm UTC](https://forums.unrealengine.com/t/using-log-math-to-make-weighted-random-number-generator/297960/2 "2014-11-02T18:38:49Z")

</div>

Actually that’s a straight forward exponential function.

For example you have your tier 10 enemy which spawns x times or x_2^0 (2^0 = 1 so x_1 which is x). You want your tier 9 to spawn twice as often so it’s x_2^1. For your tier 8 it’s x_2_2 so x_2^2. For tier 7 x_2^3 so basically you have x_2^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 🙂

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

---

<div class="post-metadata">

**Author:** ![Pierdek](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Pierdek](https://forums.unrealengine.com/u/Pierdek)\
**Post date:** [November 2, 2014, 6:39pm UTC](https://forums.unrealengine.com/t/using-log-math-to-make-weighted-random-number-generator/297960/3 "2014-11-02T18:39:48Z")

</div>

What about:

> <https://stackoverflow.com/questions/1761626/weighted-random-numbers>

---

<div class="post-metadata">

**Author:** ![Zumf](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Zumf](https://forums.unrealengine.com/u/Zumf)\
**Post date:** [November 2, 2014, 6:52pm UTC](https://forums.unrealengine.com/t/using-log-math-to-make-weighted-random-number-generator/297960/4 "2014-11-02T18:52:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Zumf](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Zumf](https://forums.unrealengine.com/u/Zumf)\
**Post date:** [November 2, 2014, 7:02pm UTC](https://forums.unrealengine.com/t/using-log-math-to-make-weighted-random-number-generator/297960/5 "2014-11-02T19:02:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Erasio](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Erasio](https://forums.unrealengine.com/u/Erasio)\
**Post date:** [November 2, 2014, 7:23pm UTC](https://forums.unrealengine.com/t/using-log-math-to-make-weighted-random-number-generator/297960/6 "2014-11-02T19:23:46Z")

</div>

Well this could be one way to do it.

 ![19824-randommathhelp.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/1/5/2/15207728cb6c0f78208f457240bc8d30890ba0fe.png)

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.

---

<div class="post-metadata">

**Author:** ![Zumf](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Zumf](https://forums.unrealengine.com/u/Zumf)\
**Post date:** [November 2, 2014, 10:07pm UTC](https://forums.unrealengine.com/t/using-log-math-to-make-weighted-random-number-generator/297960/7 "2014-11-02T22:07:22Z")

</div>

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

I’ll test it out soon. Thanks again.
