How to calculate the times to generate number for “generate number from 0 to 1 until it is greater than X...












1












$begingroup$


for example, I'm trying to test the average count for generate a number from 0 to 1 which is greater than X, let says 0.75, the program that I use (in javascript) to test it 100000 times:



let avgCount=0;
for(let i=0;i<100000;i++){
let count=0;
while(Math.random()<0.75){
count++;
}
avgCount+=count;
}
document.write(avgCount/100000);



I found it roughly needs to generate 3 numbers until it becomes greater than 0.75.



And I found if X is 0.25, it becomes roughly 0.33 (I guess it is 1/3)



the relationship between X and avgCount (average count) I guess by editing the value of X is:



0.25 --- 1/3
0.5 --- 1
0.75 ---3
0.875 --- 7
->1 --- ->infinity


My question is, how to find the relationship between X and avgCount without programming testing (e.g.:by formula)? And what is the principle of the formula, if any?










share|cite|improve this question









$endgroup$

















    1












    $begingroup$


    for example, I'm trying to test the average count for generate a number from 0 to 1 which is greater than X, let says 0.75, the program that I use (in javascript) to test it 100000 times:



    let avgCount=0;
    for(let i=0;i<100000;i++){
    let count=0;
    while(Math.random()<0.75){
    count++;
    }
    avgCount+=count;
    }
    document.write(avgCount/100000);



    I found it roughly needs to generate 3 numbers until it becomes greater than 0.75.



    And I found if X is 0.25, it becomes roughly 0.33 (I guess it is 1/3)



    the relationship between X and avgCount (average count) I guess by editing the value of X is:



    0.25 --- 1/3
    0.5 --- 1
    0.75 ---3
    0.875 --- 7
    ->1 --- ->infinity


    My question is, how to find the relationship between X and avgCount without programming testing (e.g.:by formula)? And what is the principle of the formula, if any?










    share|cite|improve this question









    $endgroup$















      1












      1








      1





      $begingroup$


      for example, I'm trying to test the average count for generate a number from 0 to 1 which is greater than X, let says 0.75, the program that I use (in javascript) to test it 100000 times:



      let avgCount=0;
      for(let i=0;i<100000;i++){
      let count=0;
      while(Math.random()<0.75){
      count++;
      }
      avgCount+=count;
      }
      document.write(avgCount/100000);



      I found it roughly needs to generate 3 numbers until it becomes greater than 0.75.



      And I found if X is 0.25, it becomes roughly 0.33 (I guess it is 1/3)



      the relationship between X and avgCount (average count) I guess by editing the value of X is:



      0.25 --- 1/3
      0.5 --- 1
      0.75 ---3
      0.875 --- 7
      ->1 --- ->infinity


      My question is, how to find the relationship between X and avgCount without programming testing (e.g.:by formula)? And what is the principle of the formula, if any?










      share|cite|improve this question









      $endgroup$




      for example, I'm trying to test the average count for generate a number from 0 to 1 which is greater than X, let says 0.75, the program that I use (in javascript) to test it 100000 times:



      let avgCount=0;
      for(let i=0;i<100000;i++){
      let count=0;
      while(Math.random()<0.75){
      count++;
      }
      avgCount+=count;
      }
      document.write(avgCount/100000);



      I found it roughly needs to generate 3 numbers until it becomes greater than 0.75.



      And I found if X is 0.25, it becomes roughly 0.33 (I guess it is 1/3)



      the relationship between X and avgCount (average count) I guess by editing the value of X is:



      0.25 --- 1/3
      0.5 --- 1
      0.75 ---3
      0.875 --- 7
      ->1 --- ->infinity


      My question is, how to find the relationship between X and avgCount without programming testing (e.g.:by formula)? And what is the principle of the formula, if any?







      probability






      share|cite|improve this question













      share|cite|improve this question











      share|cite|improve this question




      share|cite|improve this question










      asked Dec 14 '18 at 2:38









      mmmaaammmaaa

      1061




      1061






















          1 Answer
          1






          active

          oldest

          votes


















          1












          $begingroup$

          The probability of succeeding in each attempt is $p=1-x$.



          The number of tries until a success follows a geometric distribution whose mean is $1/p$ -- but that nice formula also counts the final attempt that succeeds, whereas your program doesn't.



          Correcting for this, we get that what your program ought to find is the relation
          $$ x quadmapstoquad frac{1}{1-x} - 1 = frac{x}{1-x} $$
          which agrees exactly with your observations/guesses.






          share|cite|improve this answer









          $endgroup$













            Your Answer





            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("mathjaxEditing", function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
            });
            });
            }, "mathjax-editing");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "69"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            noCode: true, onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3038860%2fhow-to-calculate-the-times-to-generate-number-for-generate-number-from-0-to-1-u%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1












            $begingroup$

            The probability of succeeding in each attempt is $p=1-x$.



            The number of tries until a success follows a geometric distribution whose mean is $1/p$ -- but that nice formula also counts the final attempt that succeeds, whereas your program doesn't.



            Correcting for this, we get that what your program ought to find is the relation
            $$ x quadmapstoquad frac{1}{1-x} - 1 = frac{x}{1-x} $$
            which agrees exactly with your observations/guesses.






            share|cite|improve this answer









            $endgroup$


















              1












              $begingroup$

              The probability of succeeding in each attempt is $p=1-x$.



              The number of tries until a success follows a geometric distribution whose mean is $1/p$ -- but that nice formula also counts the final attempt that succeeds, whereas your program doesn't.



              Correcting for this, we get that what your program ought to find is the relation
              $$ x quadmapstoquad frac{1}{1-x} - 1 = frac{x}{1-x} $$
              which agrees exactly with your observations/guesses.






              share|cite|improve this answer









              $endgroup$
















                1












                1








                1





                $begingroup$

                The probability of succeeding in each attempt is $p=1-x$.



                The number of tries until a success follows a geometric distribution whose mean is $1/p$ -- but that nice formula also counts the final attempt that succeeds, whereas your program doesn't.



                Correcting for this, we get that what your program ought to find is the relation
                $$ x quadmapstoquad frac{1}{1-x} - 1 = frac{x}{1-x} $$
                which agrees exactly with your observations/guesses.






                share|cite|improve this answer









                $endgroup$



                The probability of succeeding in each attempt is $p=1-x$.



                The number of tries until a success follows a geometric distribution whose mean is $1/p$ -- but that nice formula also counts the final attempt that succeeds, whereas your program doesn't.



                Correcting for this, we get that what your program ought to find is the relation
                $$ x quadmapstoquad frac{1}{1-x} - 1 = frac{x}{1-x} $$
                which agrees exactly with your observations/guesses.







                share|cite|improve this answer












                share|cite|improve this answer



                share|cite|improve this answer










                answered Dec 14 '18 at 2:56









                Henning MakholmHenning Makholm

                240k17305542




                240k17305542






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Mathematics Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    Use MathJax to format equations. MathJax reference.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3038860%2fhow-to-calculate-the-times-to-generate-number-for-generate-number-from-0-to-1-u%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Wiesbaden

                    Marschland

                    Dieringhausen