Home > Back-end >  Using random function rand
Using random function rand

Time:09-28


Mora game
The usage of the self-study random function rand (), using random Numbers. Write a scissors, stone, cloth, one input ([R) jock stone, [P] aper paper (cloth), [S] eissors scissors), computer synchronization output countermeasures,
Background can control three results:
(1) win equal opportunity:
(2) were accounted for only 20% of the likely win;
(3) Л always lose,
Tip: x=rand0%100, returns a random number within 0-99 range,
(1) when x between 0-33, computer output stone: 34-66, the output of cloth: between 67-99 output scissors, no matter how people, equal probability, (2) people enter the scissors, for example, when x between 0-19, computer output (win), other computer output range of stone (people lose, the probability of 80%), (3) people always lose is simple, not random number,

CodePudding user response:

Please refer to the "c + + produces a random integer method (srand () function and the rand () function)"
Wish I could help you!

CodePudding user response:

Multi-purpose a few judgment is not good? This is not difficult to oh, look at yourself, a header file time, srand (time (NULL)); Main add words of the first, follow the system time to get a random number, to ensure that is not the same, and then enter the person's input, the judge () to judge the control in the execution of the statement to determine the background of is what
Is probably the if (STRCMP (STR, 'R')==0) {{if the background control 1 x=rand () % 100; . {} the if (2) x=rand () % 100; . }};
  • Related