+ Reply to Thread
Results 1 to 11 of 11

How to generate random numbers based on a condition.

  1. #1
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,648

    How to generate random numbers based on a condition.

    hi,

    Is is possible to generate random numbers in cells say A1 and B1 so that mod(B1,A1)=0 ?

    For example I want a random number is cell B1 which is exactly divisible by another random number in cell A1.

    Is is possible in excel to do this ? If yes, please help me.

    Thanks in advance.

    sktneer

  2. #2
    Forum Expert dilipandey's Avatar
    Join Date
    12-05-2011
    Location
    Dubai, UAE
    MS-Off Ver
    1997 - 2016
    Posts
    8,191

    Re: How to generate random numbers based on a condition.

    HI Sktneer,

    See the attached file and click on the blue button to generate your desired number.
    Random number generation for MOD function.xlsm
    I wrote below code to achieve the desired results;-
    Please Login or Register  to view this content.

    Regards,
    DILIPandey
    <click on below * if this helps>
    DILIPandey, Excel rMVP
    +919810929744 (India), +971528225509 (Dubai), [email protected]

  3. #3
    Forum Contributor
    Join Date
    05-08-2013
    Location
    California
    MS-Off Ver
    Excel 2013
    Posts
    102

    Re: How to generate random numbers based on a condition.

    I hope I am not misunderstanding you, but imagine the follow scenario. There are two urns. In one urn, I have the set of strictly positive integers: 1, 2, 3, 4, and so on. In the second urn, I have the same set of strictly positive integers. Let us say that I reach into the first urn and pick out a number. If I then go to the second urn and pick out a number, and the draws are independent, then is there any way to guarantee that the first number is precisely divisible by the second number? It does not look like there ought to be any reason why it should. I could pick 9 from the first urn and a 2 in the second urn. There is nothing in this urn example that guarantees that the first number is divisible by the second number. So without any more structure to the problem, the answer is no, there is no way to guarantee that Mod(first number, second number) = 0.

  4. #4
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,648

    Re: How to generate random numbers based on a condition.

    Thanks Dilip for your reply.
    But I am unable to understand the code you have written.
    Is there any way to write a formula within the worksheet to achieve this?
    Thanks for taking pain.
    Please let me know if I can write a formula to get the desired random numbers.
    Thanks.

  5. #5
    Forum Expert dilipandey's Avatar
    Join Date
    12-05-2011
    Location
    Dubai, UAE
    MS-Off Ver
    1997 - 2016
    Posts
    8,191

    Re: How to generate random numbers based on a condition.

    Hi sktneer,

    Since you need to check a single suitable value among multiple values that would be entered in a cell, hence formula would not work and as per me, only solution is vba (macro). Thanks.

    To understand the macro, open the excel, press alt + F11, adjust the new screen (code screen) so that you can see excel worksheet and code screen - both. Now keep pressing f8 and see how code gets executed line by line


    Regards,
    DILIPandey
    <click on below * if this helps>

  6. #6
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: How to generate random numbers based on a condition.

    well if your not woried about the divisor being an integer you could just put something like

    =RANDBETWEEN(1,100) in a1 and
    =A1/RANDBETWEEN(1,100) in b1
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  7. #7
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,648

    Re: How to generate random numbers based on a condition.

    Thanks Dilip,
    No doubt, you are a genius man.

  8. #8
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,648

    Re: How to generate random numbers based on a condition.

    Thanks Martin for your help. But I wanted an integer in the divisor's place.
    Thanks.

  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: How to generate random numbers based on a condition.

    VBA seems like overkill for this.

    A1 =randbetween(1,100)

    B1 =A1*randbetween(1,100)
    Entia non sunt multiplicanda sine necessitate

  10. #10
    Forum Expert dilipandey's Avatar
    Join Date
    12-05-2011
    Location
    Dubai, UAE
    MS-Off Ver
    1997 - 2016
    Posts
    8,191

    Re: How to generate random numbers based on a condition.

    you are welcome sktneer

    Please mark this thread as [SOLVED].. thanks.


    Regards,
    DILIPandey
    <click on below * if this helps>

  11. #11
    Forum Guru sktneer's Avatar
    Join Date
    04-30-2011
    Location
    Kanpur, India
    MS-Off Ver
    Office 365
    Posts
    9,648

    Re: How to generate random numbers based on a condition.

    Hi shg,

    Thanks a lot dear. You made it very simple and perfect that's what I really wanted. Moreover I am surprised that how simple is this approach to achieve the desired results. You are super GENIUS.

+ 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