I'm currently working on a game, with scrollable screen and I need to find a simple algorithm for placing obstacles in the game. I have a gameSpeed, that is increased in time (from 1 to 12, increased by 0.005 each 1/60s) and a range of available positions between 200 and 600 (ints). I'd like to achieve a bigger probability of receiving smaller number when the speed is bigger, but it's my 14th hour straight coding and I cannot come up with anything usable and not overcomplicated. I'd like to minimize Math and random functions so that the rendering loop won't take too long. Any help appreciated !
Mar 26, 2022 1,449 views

## 1 answer to this question.

To move the density in one way, you may square or square-root the random number. Math.random()*Math.random() will produce smaller integers (around 0) with a higher chance than larger ones (near 1).
Your formula could be something like this:

`var position = Math.pow(Math.random(), gameSpeed / 3) * 400 + 200;`

Or
Make an array with more lower values than higher ones, for example, to generate random integers between [1,5]. (both inclusive). As an example, your array could be [1,1,1,1,1,2,2,2,3,3,3,4,4,5].

And if you pick an element from that array at random, you'll have a better probability of picking a low number than a high one.

Ignite Your Future with Machine Learning Training

• 5,480 points

## Create random numbers with left skewed probability distribution

I would like to pick a number ...READ MORE

## Getting one word as caption with zero probability using pretrained checkpoints for image captioning-im2txt

captiongenerator.py is a Python script that generates ...READ MORE

## How to simulate first passage time probability in python for a random walk?

To begin with, you're now computing fp ...READ MORE

## Crypto algorithm list

The learning here is that cyphers and ...READ MORE

## How do I check if an array includes a value in JavaScript?

What is the shortest and most efficient ...READ MORE

## Generate random number between two numbers in JavaScript

Is it possible to use JavaScript to ...READ MORE

## Generating random whole numbers in JavaScript in a specific range

How can I create random whole numbers ...READ MORE