Simple algorithm for generating random numbers with bigger smaller probability

0 votes
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 in Machine Learning by Dev
• 6,000 points
2,224 views

1 answer to this question.

0 votes

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

answered Apr 4, 2022 by Nandini
• 5,480 points

Related Questions In Machine Learning

0 votes
0 answers

Create random numbers with left skewed probability distribution

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

Mar 25, 2022 in Machine Learning by Dev
• 6,000 points
1,367 views
0 votes
1 answer

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

Under caption_generator.py change the following line as follow ...READ MORE

answered Feb 27, 2022 in Machine Learning by CoolCoder
• 4,420 points
692 views
0 votes
1 answer

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

answered Mar 17, 2022 in Machine Learning by Dev
• 6,000 points
523 views
0 votes
1 answer

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

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

answered Apr 5, 2022 in Machine Learning by Dev
• 6,000 points
1,673 views
0 votes
1 answer

Crypto algorithm list

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

answered Apr 7, 2022 in Blockchain by Aditya
• 7,680 points
1,400 views
0 votes
0 answers

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

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

Sep 28, 2022 in Java by Nicholas
• 7,760 points
614 views
0 votes
0 answers

Generate random number between two numbers in JavaScript

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

Sep 28, 2022 in Java by Nicholas
• 7,760 points
748 views
0 votes
0 answers

Generating random whole numbers in JavaScript in a specific range

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

Sep 28, 2022 in Java by Nicholas
• 7,760 points
643 views
0 votes
1 answer

Random numbers with user-defined continuous probability distribution

You can use the rejection sampling approach ...READ MORE

answered Mar 25, 2022 in Machine Learning by Nandini
• 5,480 points
1,422 views
0 votes
1 answer

Plotting joint probability of two random variable choices

Create pandas.DataFrame , you can use seaborn.jointplot ...READ MORE

answered Mar 23, 2022 in Machine Learning by Nandini
• 5,480 points
1,557 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP