Simulation Algorithms _ Dice games && guessing games

Source: Internet
Author: User

The simulation algorithm uses random functions to simulate unpredictable situations occurring in nature, and C uses the Srand () and Rand () functions to generate random numbers.

Let's start by introducing the generation of random numbers:

1. Random numbers that produce an indefinite range

function prototypes: int rand ()

Produces an integer between 0~rad_max, whose specific value is related to the system. Under Linux is 2147483647. We can see in the stdlib.h in the Include folder (Linux in the USR directory, windows in the installation directory)

1#include <stdio.h>2#include <stdlib.h>3 intMain ()4 {5         inti;6          for(i=0; i<Ten; i++)//randomly generates 10 numbers. 7         {8printf"%d\n", Rand ());9         }Ten         return 0; One}

2. Generate a random number of the specified range (generate 0 to a number of random numbers)

Radom (x) (rand ()%x) [0,x]

#include <stdio.h>#include<stdlib.h>#defineRandom (x) (rand ()% x)//get the random number of the specified range by taking the remainderintMain () {inti; intDis//generate a random number between [0, dis], note does not include dis         for(i=0; i<Ten; i++) {printf ("%d\n", Random (dis)); }        return 0;}

3. Generate random numbers for a specified range
Requirements: Specified range (m,n), random number includes m,n;

Method: Change the range (M,n) to (0,x) and transfer it back.

1) m=n. This is not a strictly random number, returning m;

2) M>n. Make pos=n, distance difference dis=m-n+1

Returns rand ()%dis+pos;

3) n>m. Make pos=n, distance difference dis=n-m+1

Returns rand ()%dis+pos;

1#include <stdio.h>2#include <stdlib.h>3#include <time.h>4 intRandom (intMintN)5 {6         intpos, dis;7         if(M = =N)8         {9             returnm;Ten         } One         Else if(M >N) A         { -pos =N; -dis = m-n +1; the             returnrand ()% dis +Pos; -         } -         Else -         { +pos =m; -dis = n-m +1; +             returnrand ()% dis +Pos; A         } at } - intMain () - { -         intI, M, N; -Srand ((int) Time (NULL)); -m =-3; inn =-7; -          for(i=0; i<Ten; i++) to         { +printf"%d\n", Random (M, n)); -         } the         return 0; *}

Here's the code for the dice game and the guessing game:

Dice Game:

#include <stdio.h>#include<time.h>intMain () {intm,i,n=0, s=0;    Srand (Time (NULL)); printf ("Please enter the number of times you want to roll the dice:"); scanf ("%d",&m);  for(i=1; i<=m;i++) {n=rand ()%6+1;//generate six-bit random faces of the diceprintf"the number of dice in the first%d points is:%d\n", I,n); S=s+N; } printf ("you have a total of%d times dice!\n", M); printf ("The total number of dice is:%d", s); return 0;}

Game of guessing:

1 //generate a random number within 1002#include <stdio.h>//contains a function that generates a random number3#include <time.h>4 intMain ()5 {6     intM,n,i=0;//I must first assign the initial value 07 Srand (Time (NULL));8N=rand ()% -+1;//Rand generates a random number of up to 65,000, divided here by9      Do{Tenprintf"Please enter the number you guessed:"); Onescanf"%d",&m); Ai++; -         if(m>N) -printf"The number you guessed is too big! \ n"); the         Else if(m<N) -printf"The number you guessed is too small! \ n"); -} while(m!=n); -printf"you guessed it%d times,", i); +     if(i<=5) -printf"you're so smart! "); +     Else Aprintf"you have to do more! "); at     return 0; -}

  

Simulation Algorithms _ Dice games && guessing games

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.