Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use javascript random numbers

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to use javascript random numbers, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Let's take the linear congruence algorithm as an example to analyze why random functions may still be misused. Isn't it random?

In game development, it is generally designed to open the treasure chest. Assuming that the probability of opening An and B of each treasure box is 30% and 70% respectively, the treasure box can be opened repeatedly.

Our code might be written like this.

Int open_box (box * b)

{

Int n = rand () 1000

Return n

< 300 ? b->

A: B-> b

}

Yes, this code is the root cause of the existence of "knife cushion" in opening the treasure chest.

Let's look at the definition of linear congruence (LCG) pseudorandom algorithm:

Nj+1 = (A*Nj + B) (mod M) (j, jin1 is the subscript)

The linear congruence sequence generating constant is defined by A ~ (th) B ~ (th) M ~ (- 1).

The LCG cycle is defined as the relationship between MMaga and BMagi M as follows:

1. Bmaine M coprime

2. All prime factors of M are divisible by Amurl 1.

3. If M is a multiple of 4, so is Amuri 1.

4. A, B, N0 are all smaller than M

5. A _ B is a positive integer

The popular point is that the number of [05m] generated by linear congruences is statistically equal probability. That is to say, after enough random times, they appear the same number of times.

At first glance, there seems to be nothing wrong with the above code. Because [0memm] appears with equal probability, the value after rand () 00 also appears with equal probability.

But! We overlooked the fact that this code means. Everyone's treasure chest (and even other systems) share a pseudorandom sequence.

Suppose the pseudorandom sequence of rand () 00 looks like this:

900,1,300, 500, 299, 785, 556...

Let's simulate the behavior of multiple treasure chests opening alternately:

Open the treasure box 1 and rand () 00 returns 900, so what comes out is B.

Opening the treasure box 2 and rand () 00 returns 1, so what comes out is A.

Opening the treasure box 1 ~ () rand () 00 returns 300, so what comes out is B.

Open the treasure box 1 and () 00 returns 500, so what comes out is B.

When opening the treasure box 2, rand () 00 returns 299, so what comes out is A.

If chest 1 and Box 2 have been opened alternately in a similar order. Even if you open it many times, it will be difficult for you to pat your chest and say that the probability distribution of Agraine B coming out of chest 1 and Box 2 is in line with expectations.

After all, you tell the player yourself that there is a 30% chance that every treasure box will come out of A, but Box 1 will never open A.

The reason things turned out like this. The fundamental reason is that in addition to a pseudo-random sequence, there is also a true random event, that is, the timing of the player opening the treasure chest.

In the words of software engineering, Box 1 and Box 2 are coupled by a global variable (the same linear congruence sequence), and they are not orthogonal. Therefore, opening one treasure chest is bound to affect another, so it must be wrong.

There are many similar situations, such as the trigger probability of a skill. What we told the player was that each skill was triggered with a certain probability, but we probably did it, releasing a skill with a certain probability.

Before we use random functions, we must first ask ourselves that all places that use the rand () function actually share the same pseudo-random sequence. Is that really okay?

The above is all the contents of the article "how to use javascript Random numbers". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report