In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
The knowledge points of this article "how to use C++ shuffle algorithm to generate minefields" are not quite understood by most people, so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use C++ shuffle algorithm to generate minefields" article.
Introduction
First of all, let's take a look at a question: there is an array of 100 with elements from 1 to 100. Randomly select 50 unrepeated numbers from the array.
With Math.random () * 100, you can get a random number from 0 to 99. Can you repeat it 50 times? Of course not, if, the first time random to 5, the second time if random to 5 again, the requirement is to choose not to repeat the number, so to choose 50 non-repetitive numbers, the random number is much greater than 50, because the more random to the following number, the greater the probability of repetition with the previously selected number.
How to solve it? Everyone has played or seen dealing cards. 54 cards are dealt, one card is dealt, and the dealer has one less card until all the cards have been dealt.
The time complexity is O (n) and the space complexity is O (1). The disadvantage is to know the array length n.
Code:
Void Knuth_Durstenfeld_Shuffle (vector&arr)
{
For (int i=arr.size ()-1 * > = 1 *
{
Srand ((unsigned) time (NULL))
Swap (arr [rand ()% (iTun1)], arr [I])
}
}
Shuffle algorithm generates minefields:
The minefield map is generated by disrupting the arranged mines with the shuffle algorithm.
For (int iTunes Numbai MULI I > = 0 Ted iMUR -)
{
Int iX = iUnigram M; / / iX is the X coordinate
Int iY = I% M; / / iY is Y coordinate
Int randNumber = (int) (Math.random () * (iTun1))
Int randX = randNumber/M
Int randY = randNumber%M
Swap (iX,iY,randX,randY)
} the above is about the content of this article on "how to use C++ shuffle algorithm to generate minefields". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.