Well, since a few days ago, I wrote a blog post "I used a shuffling algorithm" http://tonyxiaohome.blog.51cto.com/925273/302220), many netizens discussed with me and proposed their own algorithms. Here I want to explain one thing. I really welcome everyone to discuss purely technical issues with me. I naturally don't have a good face when it comes to malicious PK. However, we still welcome technical discussions. I have never said that my things are done, nor that my things are holy, and cannot be changed at all, whether in the past, the present or the future, I welcome everyone to discuss technical issues with me. You can also criticize them. I will change it as long as they are justified. Almost every blog post I have said, "let's say, you are welcome to shoot bricks. Well, what about this friend? To tell the truth, the algorithm is fine. But what about this Code? I am a little dizzy: Code:
- # Include <stdio. h>
- # Include <stdlib. h>
- # Include <time. h>
- Int d [6];
- Int I, n, a, B, t;
- Int c, j;
- Void Wash ()
- {
- Srand (unsigned int) time (NULL ));
- Printf ("shuffle 0 .. n-1 demo \ n ");
- For (n = 1; n <= 5; n ++)
- {/* Test 1 ~ 5 elements */
- Printf ("_____ n = % d _____ \ n", n );
- J = 1;
- For (c = 1; c <= n; c ++)
- J = j * c;/* j is n! */
- J * = n * 2;
- For (c = 1; c <= j; c ++)
- {/* Test n * 2 * n! Times */
- For (I = 0; I <n; I ++)
- D [I] = I;/* enter 0 ~ N-1 */
- For (I = n; I> 0; I --)
- {/* Disrupt 0 ~ N-1 */
- A = I-1;
- B = rand () % I;
- If (! = B)
- {
- T = d [a];
- D [a] = d [B];
- D [B] = t;
- }
- }
- Printf ("% 04d:", c );
- For (I = 0; I <n; I ++)
- Printf ("% d", d [I]);
- Printf ("\ n ");
- }
- }
- Printf ("shuffle 1 .. n demo \ n ");
- For (n = 1; n <= 5; n ++)
- {/* Test 1 ~ 5 elements */
- Printf ("_____ n = % d _____ \ n", n );
- J = 1;
- For (c = 1; c <= n; c ++)
- J = j * c;/* j is n! */
- J * = n * 2;
- For (c = 1; c <= j; c ++)
- {/* Test n * 2 * n! Times */
- For (I = 1; I <= n; I ++)
- D [I] = I;/* fill in 1 ~ N */
- For (I = n; I> 1; I --)
- {/* Disrupt 1 ~ N */
- A = I;
- B = rand () % I + 1;
- If (! = B)
- {
- T = d [a];
- D [a] = d [B];
- D [B] = t;
- }
- }
- Printf ("% 04d:", c );
- For (I = 1; I <= n; I ++) printf ("% d", d [I]);
- Printf ("\ n ");
- }
- }
- }
To be honest, I have re-typed the layout, and the original version is still dizzy. Haha. I watched it for 15 minutes and finally confirmed that I was dizzy. Only one reply is sent to him:
After reading this, I can work. I can see it in 1 ~ 5. The program itself is successful in disorder.
But to be honest, I did not understand the principle, mainly because the program is too hard to read.
Based on my programming experience, I would like to give some suggestions for your reference only:
1. My team has the same quality requirements for the test code and the formal code. You cannot relax the requirements for the test code, all requests must be implemented in strict accordance with the error-free programming method in chapter 3 of 0bug-C/C ++ commercial engineering.
2. We do not recommend that you use global variables. We recommend that you assign initial values to all variables to avoid "wild variables". Therefore, there is an inference that each row only processes the declaration of one variable, this looks very clear.
3. The array is defined as 6, more than 5, and d [6]. I have never understood what this extra cell is for. If it is just to prevent overflow errors, it should be to revise your own program to avoid overflow, rather than using this patching method.
4. My C/C ++ error-free programming method stipulates that for has only one writing method, which makes sense. for (I = 0; I <n; I ++), which implies that all n cycles, I are from 0 to n-1, exactly match the 0 ~ of array units such as d [5 ~ 4. We recommend that you use this unified format for later. Like the current for (n = 1; n <= 5; n ++), to be honest, I look at it as big as I keep thinking, what is d [0] Doing?
5. My C/C ++ error-free programming method emphasizes code simplification, and each function only writes one loop internally. The purpose is to simplify the code and make it easy to understand. There are not many functions, the fear is that the function is too long. We recommend that you split the function later.
6. My C/C ++ error-free programming method emphasizes that the function name and variable name are strictly named to write every sentence like an English short sentence, it looks clear to everyone to avoid readers from misunderstanding. I am in a daze for 15 minutes. To tell the truth, I am dizzy.
7. This is a complex logic. We recommend that you comment on the previous section to illustrate the principle you are using, and give an example to illustrate it. In this way, I can see at least a context, I can hardly understand it.
8. Where can I tell the differences between the two codes?
9. For comments in the middle of the program, we recommend that you use the C ++ mode, that is, "//". In this way, when I debug and need to temporarily hide a large code segment, you can use "/*... */", this is a habit of me and also a habit of the team. Everyone thinks it is very useful, as it is now"/*... */"when I have been widely used, it is very difficult for me to hide the code.
10. "for (c = 1; c <= n; c ++) j = j * c;/* j is n! */J * = n * 2; "I have never understood this section. Why do I have to multiply n again after the factorial? What is the mathematical principle?
11. "t = d [a]; d [a] = d [B]; d [B] = t;", which is obviously an exchange algorithm, in addition, the program appears twice. According to the regulations of our team, each logic can only appear once, write a pair at a time, and only call it later without rewriting to avoid unnecessary mistakes, we recommend that you develop a function independently, even inline.
12. "for (I = n; I> 0; I --) {/* disrupt 0 ~ N-1 */a = I-1; B = rand () % I; "This section looks very dizzy, because I know that when you assigned a value before, only 1 ~ 5. These five units are assigned values. Obviously, the value range of a is n-1 ~ 0 is 4 ~ 0. I think there is a bug in the algorithm here. No.
So much for me, the level is limited, and the depth of this algorithm may not be understood for the time being. Therefore, we cannot provide very good algorithm suggestions for the time being.
Welcome to the discussion.
Well, I think this code is quite representative. If you don't disagree, I 'd like to post a blog post to list this code and learn from it.
What do you mean?
However, this friend is obviously a very serious friend, and he then replied to me:
For example:
Several cards with a piece of paper at the top.
① Move the paper to the bottom of the card below it, that is, the next card. If the note has reached the bottom of all cards at this time, it will end.
② Randomly select one of the cards under the one on the top and the one on the other. Of course, if you choose the one on the top of the paper, you don't have to switch)
Repeat steps ① and ② until the end.
The reason for defining multiple arrays is as follows ~ N-1 and 1 ~ Code of n two subscript ranges. Because the person who references this Code may be able to choose either of the two cases for use on different occasions.
There is no difference in the code algorithms between the two ends, but it only indicates that when the subscript range is different, it is written differently.
Note I used to adopt the // style. When compiling in Win-TC, the system prompts that this style is not supported, so I will automatically replace it with/**/style.
When you reply to the post, a long sentence also means that in this special case, the/**/style is better than the // style. Of course, I still support the // style.
N * 2 * n is used for testing! I just started to use n * n! Get all the sorting results. The result fails in n hours, so you can change it to n * 2 * n at will! There is no mathematical reason.
As you have many suggestions, it is far from enough to directly use this code as part of a project code.
But it is used to demonstrate ~ N-1 and 1 ~ The array of n two subscript ranges is shuffled. It should be enough except that there is no comment on the principle description.
I am very happy to discuss with you.
I am happy to see this code in your new blog.
At this time, I really understand, um, my reply is as follows:
In this way, I understand.
You traverse each element of the array in sequence with a pointer to make sure that each element has at least one switch, which makes the card worse.
My method is to randomly select two elements. Due to the uncertainty of the random number, I do not guarantee that each element has at least one opportunity to be shuffled. It seems that, from the perspective of pure washing, my method is not as good as yours.
Well, when I first set this double random exchange, I had a consideration, because we usually wash the disk, the card is divided into two halves, and the thumb jumps out randomly, in fact, it is a random pair of cards. At this time, sometimes we can just give it a thumbs up. In fact, there may be several cards that are popped up, that is, the order is not disrupted. When we wash some dirty old cards, because there is adhesion in the middle of the card, it is easy to appear.
I was thinking about double randomness. I didn't traverse the random order in sequence. I just wanted to simulate this situation. I thought: "One or more cards are not washed, in fact, it is also a reasonable situation for random shuffling in life. "Therefore, I did not deliberately traverse it to ensure that every card has a chance to be washed. Its root cause is this.
Well, can you understand what I mean when I say this? The original location information is not washed, but also a manifestation of randomness. Since it is random, that is to say, it is random if it cannot be selected or washed.
Therefore, I chose the shuffling method I mentioned above.
Well, you are welcome to discuss this.
This is the result of our discussion on the shuffling algorithm so far. I thought about it and thought it was necessary to write a blog post on the details of the information, this helps you understand random numbers. Here I would like to share my opinion: When a random number is solved, the most important thing is that we cannot predict the position, that is, we cannot assert that a certain number exists or assert that a certain number does not exist, these are not real random numbers, they are pseudo random numbers that have been affected by the ideas of other parameter programmers. For example, we dumped the dice. we dumped the first six points, and the second was not necessarily six points. We all know that, otherwise, it would not be a random number. However, I found that few programmers and friends pay attention to the details. At the same time, we cannot exclude the six points in advance, that is, the second result must be "not" 6 points, which is not random. My random shuffling algorithm uses double random position switching. I don't want to preset positions. I don't want a card to be switched. However, at the same time, I don't want a card to be exchanged. I don't think this is really random, so I chose the current algorithm. This friend's algorithm has some predefined positions in it. I don't think it's random enough. Well, let's talk about it. You are welcome to continue to discuss it. ========================================================== ====================
Buy 0bug-C/C ++ commercial engineering at the reserve price online
Directly click the link below or copy it to the address bar of the browser)
Http://s.click.taobao.com/t_3? Limit % 3 Fpid % 3Dmm_13866629_0_0 Xiao Miao
This article from the "Xiao blog" blog, please be sure to keep this source http://tonyxiaohome.blog.51cto.com/925273/311861