The random command is used to initialize a hero at random. The random command is used to select a hero at random.

In JavaScript, for example, we usually use the Random method of the Math library to get a random number from 0 to 1.

Math.random()
Copy the code

There is a concept called pseudorandom, which means literally, not real and unpredictable randomness, but randomness generated by certain simulation algorithms, and the results generated by these algorithms are certain, which is called pseudorandom.

Usually, the result from Random is not what we want. In this case, a numerical transformation will be performed to map the given range 0-1 to our target range.

Products: happy today, give all users a sunny red envelope, the amount from 0 to 100W random.


Arvit: Ok. (Stupid product)

function valueMapping (x, inMin, inMax, outMin, outMax) {
  return (x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
}
valueMapping(Math.random(), 0, 1, 0, 100w)
Copy the code

This kind of randomness of equal distribution is common. But products don’t like to be so random.

Product: I want 50% of the people to fail, and then a 30% chance of winning the third prize, a 15% chance of winning the second prize, and a 5% chance of winning the first prize. Can you do that?


Arvit: Look down on me?? Is that difficult?

Simple needs must be met, of course, the conditions of this lottery are limited, we can use the way to retrieve ping-pong balls from the container. As long as you put balls in the container in proportion to, say, 100 balls, 50 of them won’t win, then you pick one at random and you have a 50% chance of not winning.

let container = [1, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
var random = container[Math.floor(Math.random() * container.length)]
Copy the code

More fickle than women in the world, is the woman who makes products.

Product: I think…


Arvit: I see. We can do it.


Product: How do you know what I’m going to say?


Arvit: Experience.

The product said that the previous random algorithm was too stupid. It wanted to be linearly related to data integrity. Users with higher data integrity were more likely to be selected. Data integrity is a number from 0 to 1.

function randomFuck (completingRate) {
  return Math.random() < completingRate
}
Copy the code

Product: What about quadratic correlation?


Arvit: My time is precious, can you say it all at once? (Playing blue moon on the line…)

function randomFuckTwice (completingRate) {
  return Math.random() * Math.random() < completingRate
}
Copy the code

Product: Someone else’s sunny draw curve is much better. It is said to be a normal distribution with a median of 100 and a variance of 10. Let’s copy it?


Arvit: (blue ‘д) Blue roll

import randomNormal from 'random-normal'
randomNormal({mean: 100, dev: 10})
Copy the code

Gaussian distribution for fog virus effect:

Product: lucky draw so, do not change


Arvit: Niceeeeeeeee


Product: We do the next requirement


Arvit:…


Product: I want the ball position to change randomly. The requirement is very simple. The next random position is related to the last one, not a few random jumps.


Arvit:…

Perlin Noise is a natural Noise generation algorithm invented by Ken Perlin. The PerlinNoise algorithm produces two random numbers with a slight correlation between them, usually used to generate some texture effects. Perlin Noise Demo:

Recently, I started to read The Book The Nature Of Code again. I bought it at The beginning Of 2017. I read it quickly and gained a lot, but I missed many knowledge points.

The above. The summary content is relatively simple, but as Arvit said, learn more methods on the body, so that the next time when facing the needs of product managers can be more easily, this is our external weapon.

A link to the

  • p5.js
  • random-normal
  • The Nature Of Code

If reproduced, please indicate the source:
w3ctrain.com / 2018/02/12/random-func-sum-up/

My name is Zhou Xiaokai

I am a front-end development engineer now, and I am still a rookie in the way of programming. W3ctrain is the place where I record my learning and growth.