C-language random number in the problem of throwing dice

Source: Internet
Author: User

I've seen someone write "Dice 100 times, print out the dice" code

Then find empty write a bit of code, not very good, just for everyone to discuss

Because the results of the dice are randomly generated between 1~6, the code must also simulate random situations

But the computer can't really generate random numbers, it can only be generated in a function approximation, so my code is written in this way.

There are two steps to getting a random number, and the first step is to set the seed that generates the random number, and the second step is to get the random number from the seed.

Each of these two steps corresponds to two functions: Srand () and Rand ()

And because it's a function, the nature of the function determines that if the seed is the same, it will get the same result, and there will be no random situation.

To avoid this, use time as the seed of the random number, which is used in this case

The code is as follows:

#include <stdio.h>#include<stdlib.h>//Srand and Rand functions are used#include <time.h>//using the time functionintMain () {//set the seed for generating random numbers on timeSrand ((unsigned) time (NULL)); //sets an int array of length 6 to hold the result of the dice    intnumbers[6] = {0,0,0,0,0,0}; //Cycle Dice 100 times     for(inti =0; I < -; i++) {        //generate random results, control between 1~6        intnum = rand ()%6+1; //According to the results of the corresponding statistics        Switch(num) { Case 1://The dice result is a count of 1 o'clocknumbers[0]++;  Break;  Case 2://The dice result is a count of 2 o'clocknumbers[1]++;  Break;  Case 3://count when the dice result is 3numbers[2]++;  Break;  Case 4://The dice result is a count of 4 o'clocknumbers[3]++;  Break;  Case 5://count when the dice result is 5numbers[4]++;  Break;  Case 6://The dice result is a count of 6 o'clocknumbers[5]++; default:                 Break; }    }    //traversing the array output after the dice is complete     for(intj =0; J <6; J + +) {        //Output Resultsprintf"%d", Numbers[j]); if(J <5) {printf (" - "); }        Else{printf ("\ n"); }    }        return 0;}

It is important to note that because the computer is running very fast, the seed cannot be set in the For loop

Otherwise it will cause many seeds to be the same, resulting in the same consequences as the resulting value

This article refers to the following articles:

Http://blog.chinaunix.net/uid-25513153-id-200003.html

C-language random number in the problem of throwing dice

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.