+ Reply to Thread
Results 1 to 12 of 12

Random Number Counting

  1. #1
    Registered User
    Join Date
    04-22-2009
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    24

    Random Number Counting

    Hi,

    If I generate a random number in cell A1 ranging from 1 to 3
    Can I count how many times the result is equal to 2
    if I was to run it 100 times and then show the results in cell A3.

    Regards

    Steve
    Last edited by gadjit; 09-22-2009 at 09:01 AM.

  2. #2
    Registered User
    Join Date
    12-14-2007
    Location
    Ontario
    Posts
    19

    Re: Random Number Counting

    Please Login or Register  to view this content.

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Random Number Counting

    You don't need to actually put the random number into the cell on the sheet, that slows things down needlessly. Once you start to use VBA, you can do it all in VBA and then just put the results onto the sheet.

    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Registered User
    Join Date
    12-14-2007
    Location
    Ontario
    Posts
    19

    Re: Random Number Counting

    The
    If Evaluate("=RANDBETWEEN(1,3)") = 2 Then c = c + 1
    Gave me a type mismatch.
    anyone else get that?

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Random Number Counting

    It works for me on Excel 2003. Here's an alternate technique you can use:
    Please Login or Register  to view this content.
    Excelguy, does this even work for you:
    Please Login or Register  to view this content.
    If you don't get a popup saying "1" then you can't use the Evaluate() function.
    Last edited by JBeaucaire; 09-21-2009 at 01:22 PM.

  6. #6
    Registered User
    Join Date
    12-14-2007
    Location
    Ontario
    Posts
    19

    Re: Random Number Counting

    Yep, the Sub Test worked for me. I'll have to try the other example you gave. but if the Evaluate function worked then there should be no reason why the first example would have an issue right?

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Random Number Counting

    Quote Originally Posted by excel_guy49 View Post
    ...if the Evaluate function worked then there should be no reason why the first example would have an issue right?
    That is correct. The Evaluate() function is simply running a worksheet formula in memory instead of in a cell (much faster), so if it works, it works.

  8. #8
    Registered User
    Join Date
    04-22-2009
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    24

    Arrow Re: Random Number Counting

    Here is an example I put together after much help from the Forum, thanks for your help.

    Is there any reason I find number 1 & 10 do not appear as often as the rest?
    Attached Files Attached Files

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Random Number Counting

    Your use of ROUND halves the probabilities of the endpoints.

    If you want uniform distribution between 1 and 10,

    =int(rand()*10) + 1
    Entia non sunt multiplicanda sine necessitate

  10. #10
    Registered User
    Join Date
    04-22-2009
    Location
    london
    MS-Off Ver
    Excel 2003
    Posts
    24

    Re: Random Number Counting

    thanks, it work a treat.

  11. #11
    Registered User
    Join Date
    01-05-2013
    Location
    ta
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Random Number Counting

    Is there a simple way to improve this code so that it also tells on which attempts number 2 came up? Basically I have to create a code for throwing dice (random numbers 1-6) 100 times and count how many times number two is thrown and also on which attempts.

  12. #12
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Random Number Counting

    flm,

    Unfortunately you need to post your question in a new thread, it's against the forum rules to post a question in the thread of another user. If you create your own thread, any advice will be tailored to your situation so you should include a description of what you've done and are trying to do. Also, if you feel that this thread is particularly relevant to what you are trying to do, you can surely include a link to it in your new thread.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1