# I am trying to get a random number

**URL:** <https://discourse.codecombat.com/t/i-am-trying-to-get-a-random-number/29667>\
**Category:** Artisan\
**Created:** [July 15, 2021, 7:38pm UTC](https://discourse.codecombat.com/t/i-am-trying-to-get-a-random-number/29667 "2021-07-15T19:38:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JWH](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@JWH](https://discourse.codecombat.com/u/JWH)\
**Post date:** [July 15, 2021, 7:38pm UTC](https://discourse.codecombat.com/t/i-am-trying-to-get-a-random-number/29667/1 "2021-07-15T19:38:00Z")

</div>

I am trying to randomize my level with the placement enemies, but when I use random.random() the whole thing, doesn’t crash but is messed up.

 ![Screenshot from 2021-07-15 14-13-02](https://us1.discourse-cdn.com/flex016/uploads/codecombat/original/3X/2/5/25c68fb8f4f690f175950cc3a1db0caf0605717a.png)

---

<div class="post-metadata">

**Author:** ![Chaboi\_3000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chaboi_3000/32/48486_2.png) [@Chaboi\_3000](https://discourse.codecombat.com/u/Chaboi_3000)\
**Post date:** [July 15, 2021, 11:49pm UTC](https://discourse.codecombat.com/t/i-am-trying-to-get-a-random-number/29667/2 "2021-07-15T23:49:20Z")

</div>

It obviously won’t work because that’s not Python. Coffeescript is really a simplified version of JS, and both CS and JS use `Math.random()` to generate a random number 0 \<= x \< 1.0.

---

<div class="post-metadata">

**Author:** ![JWH](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@JWH](https://discourse.codecombat.com/u/JWH)\
**Post date:** [July 16, 2021, 12:48am UTC](https://discourse.codecombat.com/t/i-am-trying-to-get-a-random-number/29667/3 "2021-07-16T00:48:27Z")

</div>

Oh. Thanks for pointing that out.

---

<div class="post-metadata">

**Author:** ![Hydrobolic](https://avatars.discourse-cdn.com/v4/letter/h/e47c2d/32.png) [@Hydrobolic](https://discourse.codecombat.com/u/Hydrobolic)\
**Post date:** [July 16, 2021, 1:55am UTC](https://discourse.codecombat.com/t/i-am-trying-to-get-a-random-number/29667/4 "2021-07-16T01:55:55Z")

</div>

CodeCombat uses a seeded random number generator, which I would suggest using instead. The methods are defined [here](https://github.com/codecombat/codecombat/blob/master/app/lib/world/rand.coffee).

_Seeded_ as in the same results are produced from the same seed, allowing the exact same pseudo-random positions to be generated multiple times if you want to repeat a game exactly.

e.g. `this.world.rand.randf()`.

---

<div class="post-metadata">

**Author:** ![Chaboi\_3000](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/chaboi_3000/32/48486_2.png) [@Chaboi\_3000](https://discourse.codecombat.com/u/Chaboi_3000)\
**Post date:** [July 16, 2021, 2:07am UTC](https://discourse.codecombat.com/t/i-am-trying-to-get-a-random-number/29667/5 "2021-07-16T02:07:16Z")

</div>

Good point. Seeded RNG **must** be used for levels(ie. Kithgard Mastery) where playback is disabled. For forest levels and above, the differences are less important as players are given a different seed once they press submit. But usually any RNG that affects the visual-aspect should be done using seeded RNG. (For the sake of units not flying around every time users run the level)
