How many random numbers between 0 and 1 are greater than 1?

Source: Internet
Author: User
Tags mathematical constants natural logarithm
The most fascinating thing about mathematical constants is that they often appear in seemingly unrelated places. Take a random number between 0 and 1, add another random number between 0 and 1, and then add a random number between 0 and 1 ?? Until and beyond 1. An interesting question: how many times does it take to increase the sum to over 1? The answer is "e", natural logarithm.

The most fascinating thing about mathematical constants is that they often appear in seemingly unrelated places. Take a random number between 0 and 1, add another random number between 0 and 1, and then add a random number between 0 and 1 ?? Until and beyond 1. An interesting question: how many times does it take to increase the sum to over 1? The answer is "e", natural logarithm.

The following is a program simulation:

# Include
 
  
# Include
  
   
# Include
   
    
# Define NUM 11int main () {int sum = 0; int I, wait; double val, random; srand (time (NULL); for (I = 0; I
            
     

Program running result:

0.85769109320536 0th tests completed 1st tests completed 2nd tests completed 3rd tests completed 4th tests completed 5th tests completed 6th 7th tests completed tests 0.2381360.988189 8th tests completed 0.1146280.6579180.603290 9th tests completed 0.5117040.642293 10th tests completed an average of 2.636364 tests

Next we will modify the program, such as testing 10000 sets of data, and then calculate the average number of times:

# Include
      
       
# Include
       
        
# Include
        
         
# Define NUM distinct int main () {int sum = 0; int I, wait; double val, random; srand (time (NULL); for (I = 0; I
                 
          

Program running result:

2.727400 requests on average

This value is very close to the natural logarithm e, and its value is about 2.718281828.

Then we can print out the 100000 test data, and then calculate the number of test data that must be equal to or greater than one random number, two random numbers and equal to or greater than 1 are required. There are several groups of test data... Print out the distribution.

#include 
           
            #include 
            
             #include 
             
              #define TIMES 100000#define COUNT 11int main(void){int i,j;double sum,ave;long int Sum =0;int count[TIMES] = {0};int number[COUNT] = {0};srand(time(NULL));for(i=0;i
              
               NUL 2>NUL");getch(); }
              
             
            
           

Program running result:

1       4       0.0000402       49817   0.4981703       33620   0.3362004       12360   0.1236005       3344    0.0334406       744     0.0074407       94      0.0009408       14      0.0001409       3       0.00003010      0       0.0000002.719190

This article is available at http://www.nowamagic.net/librarys/veda/detail/467.

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.