+ Reply to Thread
Results 1 to 10 of 10

Formula to return random number from a list and associated name

  1. #1
    Registered User
    Join Date
    01-11-2009
    Location
    Woodland, CA
    MS-Off Ver
    Excel 2003
    Posts
    27

    Formula to return random number from a list and associated name

    My objective is to take two different random counts of numbers written in column b, ranging from B9:B66. However I want to highlight the specific fields listed in the example below, and pull random numbers from those selected fields only. Then I want to pull another set of random numbers in the same column B9:B66, however using a different specific set of fields within column B; as shown in the second random formula string. Use the Press 9 feature to randomly select the two different numbers. What this attempt is trying accommplish is selecting one boy, one girl who have assigned numbers adjacent to their names(name field populated in column c).
    What am I doing in correctly?

    HTML Code: 
    Last edited by gmaz2; 02-14-2009 at 01:21 PM. Reason: errant title

  2. #2
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Random formula-what is wrong w/this?

    I think you might just have your index range wrong...

    it should be C9:C66 if you want to pull names from Column C corresponding to unique numbers in column B.

    e.g.

    Please Login or Register  to view this content.
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

  3. #3
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,697

    Re: Random formula-what is wrong w/this?

    I don't think you'll get this to work with that approach using INDEX. You could probably use a similar approach with CHOOSE, i.e

    =CHOOSE(INT(RAND()*COUNTA(B9,B13,B15,B16,B17,B20,B21,B23,B25,B31,B33,B34,B35,B37,B38,B39,B41,B42,B44,B45,B47,B50,B51,B52))+1,B9,B13,B15,B16,B17,B20,B21,B23,B25,B31,B33,B34,B35,B37,B38,B39,B41,B42,B44,B45,B47,B50,B51,B52)

    but CHOOSE limits you to 29 arguments.

    I'd suggest using a helper column, e.g. in D9:D66 put either "B" or "G" to indicate boy or girl on each row (you can hide this column if you want) then you can use this formula to generate a random "B" number from column B

    =INDEX(B9:B66,SMALL(IF(D9:D66="B",ROW(D9:D66)-ROW(D9)+1),INT(RAND()*COUNTIF(D9:D66,"B")+1)))

    This is an "array formula" that needs to be confirmed with CTRL+SHIFT+ENTER. To do this select the cell with formula, press F2 key then hold down CTRL and SHIFT keys and press ENTER so that curly braces like { and } appear around the formula in the formula bar.

    Note: I've used RAND() rather than RANDBETWEEN to avoid ATP functions.....but you could also use RANDBETWEEN.....

    Note2: I don't know if you expect any cells to be blank in B9:B66. If there are then CHOOSE approach won't work correctly unless those blanks are progressively from the bottom of the range
    Last edited by daddylonglegs; 02-12-2009 at 09:36 AM.

  4. #4
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Random formula-what is wrong w/this?

    You are right, ddl... I should've tested it more extensively.....

  5. #5
    Registered User
    Join Date
    01-11-2009
    Location
    Woodland, CA
    MS-Off Ver
    Excel 2003
    Posts
    27

    Re: Random formula-what is wrong w/this?

    Quote Originally Posted by daddylonglegs View Post
    I don't think you'll get this to work with that approach using INDEX. You could probably use a similar approach with CHOOSE, i.e

    =CHOOSE(INT(RAND()*COUNTA(B9,B13,B15,B16,B17,B20,B21,B23,B25,B31,B33,B34,B35,B37,B38,B39,B41,B42,B44,B45,B47,B50,B51,B52))+1,B9,B13,B15,B16,B17,B20,B21,B23,B25,B31,B33,B34,B35,B37,B38,B39,B41,B42,B44,B45,B47,B50,B51,B52)

    but CHOOSE limits you to 29 arguments.

    I'd suggest using a helper column, e.g. in D9:D66 put either "B" or "G" to indicate boy or girl on each row (you can hide this column if you want) then you can use this formula to generate a random "B" number from column B

    =INDEX(B9:B66,SMALL(IF(D9:D66="B",ROW(D9:D66)-ROW(D9)+1),INT(RAND()*COUNTIF(D9:D66,"B")+1)))

    This is an "array formula" that needs to be confirmed with CTRL+SHIFT+ENTER. To do this select the cell with formula, press F2 key then hold down CTRL and SHIFT keys and press ENTER so that curly braces like { and } appear around the formula in the formula bar.

    Note: I've used RAND() rather than RANDBETWEEN to avoid ATP functions.....but you could also use RANDBETWEEN.....

    Note2: I don't know if you expect any cells to be blank in B9:B66. If there are then CHOOSE approach won't work correctly unless those blanks are progressively from the bottom of the range

    adding a column D, using the "B", "G" suggestion works-thank you, is there a way to pull the corresponding name down into column "C70" and "C72", so the name of the number holder randomly selected appears adjacent number?

    ie.

    Boys

    25 Joe Smith

    Girls

    59 Mary Smith
    Last edited by gmaz2; 02-13-2009 at 12:32 AM.

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

    Re: Random formula-what is wrong w/this?

    gmaz2,

    Please read the forum rules about thread titles before posting again.
    Entia non sunt multiplicanda sine necessitate

  7. #7
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Random formula-what is wrong w/this?

    You can change the Index range in daddylonglegs' formula as I had initially suggested...

    Please Login or Register  to view this content.
    or if you are showing the number and want the name beside it, you could use Vlookup...
    Please Login or Register  to view this content.
    where I assume X1 contains the randomly pulled number.. change as necessary.
    Last edited by NBVC; 02-13-2009 at 12:38 PM.

  8. #8
    Registered User
    Join Date
    01-11-2009
    Location
    Woodland, CA
    MS-Off Ver
    Excel 2003
    Posts
    27

    Re: How to create formula, that pulls name to match number

    I don't know why I received a violation, I changed the title to now match what I am trying to accomplish from the original question. I tried the formulas you suggested, this is what I would hope to accomplish

    in column B70 there is a number that is pulled into this field. Once the number is entered into that field, in Column C, can a formula be written to say:

    If a number is found in Column b70, then pull in the name list adjacent to that number. So if the number is pulled in from B10, pull in the name from C10? This formula would have to written with a wildcat element to?? so it can recognize number pulled into B70, and find the corresponding name in column C.

    The formulas you suggested find a random name but the name that is found does not associate to the random number selected???

    I hope I don't receive another violation notice-I am not trying to be misleading
    Thanks for your help

  9. #9
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    Microsoft 365
    Posts
    14,697

    Re: Random formula-what is wrong w/this?

    Hello gmaz2,

    shg gave you a violation for the thread title. You'd probably be better off with something like "Formula to return random number from a list and associated name"

    NBVC suggests that you could get the random number as per my suggestion and then use VLOOKUP to get the name associated with that number......but that won't work if any of the numbers are duplicates in which case it would probably be better to randomly select a name first and then get the number (this assumes that there are no duplicate names), so.....use NBVC's amendment to my formula in C70 to get a random name

    =INDEX(C9:C66,SMALL(IF(D9:D66="B",ROW(D9:D66)-ROW(D9)+1),INT(RAND()*COUNTIF(D9:D66,"B")+1)))

    and then in B70 to get the number associated with that name

    =INDEX(B9:B66,MATCH(C70,C9:C66,0))

  10. #10
    Registered User
    Join Date
    01-11-2009
    Location
    Woodland, CA
    MS-Off Ver
    Excel 2003
    Posts
    27

    Re: Formula to return random number from a list and associated name

    Thanks, the VLOOKUP Solution works. Thanks for your assistance and patience through all of this.
    Gmaz2
    Last edited by gmaz2; 02-14-2009 at 01:29 PM.

+ 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