Home

Generating Random Number, Excluding Cell in Next Calculation

%3CLINGO-SUB%20id%3D%22lingo-sub-784510%22%20slang%3D%22en-US%22%3EGenerating%20Random%20Number%2C%20Excluding%20Cell%20in%20Next%20Calculation%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-784510%22%20slang%3D%22en-US%22%3E%3CP%3EHello.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20currently%20have%20a%20column%20of%20data%20that%20I%20need%20to%20select%20a%20random%20number%20from.%20I%20have%20the%20correct%20formula%20for%20that.%20The%20issue%20is%20that%20I%20need%20to%20exclude%20the%20cell%20that%20was%20%22picked%22%20in%20the%20previous%20formula%20from%20the%20next%20result%20I%20need%20to%20generate%20(when%20I%20drag%20the%20formula%20down).%26nbsp%3B%20Here%20is%20the%20formula%20I%20am%20using%20to%20generate%20the%20random%20number%3A%26nbsp%3B%3DINDEX(%24B2%3A%24B47%2CRANDBETWEEN(1%2CCOUNTA(%24B2%3A%24B47))%2C1).%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere's%20an%20example%20if%20this%20is%20hard%20to%20understand.%20Cell%20A1%3D%203%2C%20Cell%20A2%3D4%2C%20Cell%20A3%3D5%2C%20Cell%20A4%3D6.%20My%20formula%20in%20B1%20chose%20the%20number%204.%20Now%2C%20in%20B2%2C%20I%20need%20a%20number%20that%20excludes%20cell%20A2%20(4).%26nbsp%3B%20Let's%20say%20the%20next%20number%20is%203.%20Now%20in%20B3%2C%20I%20need%20a%20formula%20that%20generates%20a%20random%20number%20but%20excludes%20both%20A1%20and%20A2.%20If%20anyone%20can%20help%2C%20please%20do!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-784510%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-784712%22%20slang%3D%22en-US%22%3ERe%3A%20Generating%20Random%20Number%2C%20Excluding%20Cell%20in%20Next%20Calculation%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-784712%22%20slang%3D%22en-US%22%3E%3CP%3EHello%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F385918%22%20target%3D%22_blank%22%3E%40John_M2205%3C%2FA%3E%26nbsp%3B%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Ewhat%20is%20the%20expected%20result%3F%20What%20does%20the%20formula%20calculate%3F%20How%20does%20%22excluding%22%20look%20like%2C%20compared%20to%20not%20excluding%3F%20Maybe%20you%20can%20create%20a%20small%20data%20sample%20and%20upload%20the%20workbook%20here.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-785883%22%20slang%3D%22en-US%22%3ERe%3A%20Generating%20Random%20Number%2C%20Excluding%20Cell%20in%20Next%20Calculation%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-785883%22%20slang%3D%22en-US%22%3E%3CP%3EHere%20is%20my%20example!%20The%20expected%20result%20is%20a%20return%20from%20the%20S%26amp%3BP%20500%20from%20a%20previous%20year.%20Once%20that%20return%20is%20generated%2C%20I%20do%20not%20want%20it%20generated%20again%20once%20I%20drag%20the%20formula%20down.%20For%20example%2C%20the%20first%20number%20generated%20from%20the%20collection%20of%20data%20(column%20b)%20was%201.06.%20Since%20that%20number%20was%20generated%2C%20I%20do%20not%20want%20that%20number%20to%20be%20considered%20when%20I%20generate%20the%20next%20random%20number.%20Thanks%20for%20your%20response%20and%20I%20appreciate%20any%20feedback!%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F7724%22%20target%3D%22_blank%22%3E%40Ingeborg%20Hawighorst%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-787465%22%20slang%3D%22en-US%22%3ERe%3A%20Generating%20Random%20Number%2C%20Excluding%20Cell%20in%20Next%20Calculation%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-787465%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F385918%22%20target%3D%22_blank%22%3E%40John_M2205%3C%2FA%3E%26nbsp%3B%2C%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20do%20this%20with%20a%20helper%20table.%20You%20basically%20want%20to%20create%20a%20list%20of%20the%2046%20row%20numbers%20sorted%20randomly%2C%20which%20you%20can%20then%20feed%20into%20the%20Index%20function.%20To%20do%20this%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E-%20create%20a%20helper%20column%20with%20the%20row%20numbers%20from%201%20to%2046%20on%20the%20same%20or%20a%20different%20sheet.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E-%20next%20to%20that%20column%20enter%20the%20%3DRand()%20function%20and%20copy%20down.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E-%20turn%20these%20two%20columns%20into%20an%20Excel%20table%3C%2FP%3E%0A%3CP%3E-%20sort%20the%20table%20by%20the%20column%20with%20the%20Rand%20function%3C%2FP%3E%0A%3CP%3E-%20you%20now%20have%20a%20randomly%20sorted%20list%20of%20the%20unique%20row%20numbers%3C%2FP%3E%0A%3CP%3E-%20point%20the%20Index%20function%20to%20the%20row%20number%20column%20of%20the%20table.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3D%40INDEX(%24B%242%3A%24B%2447%2CTable1%5B%40%5Bunique%20numbers%5D%5D)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOr%2C%20if%20you%20don't%20want%20to%20use%20structured%20referencing%2C%20you%20can%20use%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3D%40INDEX(%24B%242%3A%24B%2447%2CF2)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EEvery%20time%20you%20sort%20the%20helper%20table%2C%20the%20Rand()%20function%20will%20recalculate%2C%20so%20if%20you%20want%20to%20shuffle%20your%20numbers%2C%20just%20sort%20the%20Rand()%20column.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F125544iFF4205BB58A29A16%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%222019-08-05_09-17-36.jpg%22%20title%3D%222019-08-05_09-17-36.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYour%20original%20formula%20counts%20the%20number%20of%20entries%20in%20column%20B%2C%20so%20you%20may%20have%20a%20varying%20number%20of%20rows.%20To%20cater%20for%20that%2C%20create%20as%20many%20row%20numbers%20in%20the%20helper%20as%20you%20will%20need%20for%20your%20largest%20possible%20data%20set.%20Then%2C%20instead%20of%20just%20the%20Rand()%20function%2C%20use%20this%20formula%20for%20the%20sort%20column%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3DIF(COUNT(B%3AB)%26gt%3B%3DROW()-1%2CRAND()%2C0)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20will%20generate%20random%20numbers%20only%20for%20the%20row%20numbers%20with%20content%20and%20create%200%20values%20for%20the%20rest.%20Now%20you%20can%20sort%20from%20large%20to%20small%20to%20get%20the%20random%20order%20of%20the%20row%20numbers.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-787741%22%20slang%3D%22en-US%22%3ERe%3A%20Generating%20Random%20Number%2C%20Excluding%20Cell%20in%20Next%20Calculation%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-787741%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F385918%22%20target%3D%22_blank%22%3E%40John_M2205%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20is%20probably%20jumping%20the%20gun%20somewhat%20because%20dynamic%20arrays%20are%20only%20just%20starting%20to%20be%20released%20to%20Office%20365%20users.%26nbsp%3B%20DAs%20allow%20the%20same%20strategy%20of%20generating%20a%20set%20of%20random%20numbers%20and%20using%20their%20sort%20order%20that%20was%20used%20by%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F7724%22%20target%3D%22_blank%22%3E%40Ingeborg%20Hawighorst%3C%2FA%3Eto%20be%20applied%20directly%20to%20the%20list%20of%20values%20without%20the%20use%20of%20a%20helper%20range%20or%20index%20lookup.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3E%3CFONT%3E%3D%20SORTBY(%20ValueList%2C%20RANDARRAY(COUNT(ValueList))%20)%3C%2FFONT%3E%3C%2FSTRONG%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-788436%22%20slang%3D%22en-US%22%3ERe%3A%20Generating%20Random%20Number%2C%20Excluding%20Cell%20in%20Next%20Calculation%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-788436%22%20slang%3D%22en-US%22%3E%3CP%3EWow!%20Thank%20you%20so%20much!!!%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F7724%22%20target%3D%22_blank%22%3E%40Ingeborg%20Hawighorst%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-788536%22%20slang%3D%22en-US%22%3ERe%3A%20Generating%20Random%20Number%2C%20Excluding%20Cell%20in%20Next%20Calculation%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-788536%22%20slang%3D%22en-US%22%3E%3CP%3EI%20Have%20one%20more%20question.%20When%20you%20say%2C%20%22sort%20the%20table%20with%20the%20Rand%20function%22%20do%20you%20mean%20the%20following%20steps%3A%20click%20on%20a%20column%2C%20click%20%22sort%20%26amp%3B%20filter%22%2C%20click%20%22sort%20by%20random%20number%22%2C%20Sort%20on%20%22Values%22.%20And%20then%20what%20order%3F%20I%20am%20not%20sure%20what%20you%20mean%20when%20you%20say%20to%20sort%20by%20the%20rand%20function.%20Thank%20you!%26nbsp%3B%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F7724%22%20target%3D%22_blank%22%3E%40Ingeborg%20Hawighorst%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-789138%22%20slang%3D%22en-US%22%3ERe%3A%20Generating%20Random%20Number%2C%20Excluding%20Cell%20in%20Next%20Calculation%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-789138%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F385918%22%20target%3D%22_blank%22%3E%40John_M2205%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20think%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F7724%22%20target%3D%22_blank%22%3E%40Ingeborg%20Hawighorst%3C%2FA%3Ewill%20have%20used%20the%20RANK%20function%20to%20rank%20each%20instance%20of%20a%20random%20number%20within%20the%20list.%26nbsp%3B%20This%20will%20generate%20values%201-46%20that%20are%20used%20as%20the%20index%20(row%20number)%20parameter%20within%20the%20INDEX%20function.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20puzzled%20by%20the%20%22%40%22%20symbol%20that%20appears%20before%20the%20INDEX%20function%20in%20the%20formula%20bar%20(blocking%20an%20array%20calculation%20to%20return%20a%20single%20value).%26nbsp%3B%20Who%20is%20it%20that%20is%20using%20an%20up-to-the-minute%20version%20of%20Office%20365%3F%20%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%5BJust%20checked%20back%3B%20it%20was%20Ingeborg's%20post%20that%20included%20the%20symbol%5D%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-796088%22%20slang%3D%22en-US%22%3ERe%3A%20Generating%20Random%20Number%2C%20Excluding%20Cell%20in%20Next%20Calculation%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-796088%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F385918%22%20target%3D%22_blank%22%3E%40John_M2205%3C%2FA%3E%26nbsp%3B%20I%20meant%3A%20Sort%20the%20table%20manually%2C%20using%20the%20sort%20command%20on%20the%20column%20with%20the%20RAND%20function.%20Sort%20order%20does%20not%20really%20matter%2C%20unless%20you%20use%20the%20second%20technique%20where%20you%20have%20zero%20values%20in%20the%20column.%20In%20that%20case%2C%20sort%20descending.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-796091%22%20slang%3D%22en-US%22%3ERe%3A%20Generating%20Random%20Number%2C%20Excluding%20Cell%20in%20Next%20Calculation%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-796091%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F214174%22%20target%3D%22_blank%22%3E%40Peter%20Bartholomew%3C%2FA%3E%26nbsp%3B%20Yes%2C%20I%20use%20Office%20Insider%2C%20which%20already%20includes%20the%20new%20Dynamic%20Array%20functions.%20It's%20sometimes%20hard%20to%20write%20functions%20the%20old%20way%2C%20because%20things%20now%20just%20work.%20The%20backwards%20compatibility%20is%20achieved%20with%20the%26nbsp%3B%40%20symbol%20that%20tells%20a%20function%20not%20to%20use%20the%20new%20array%20way%20of%20things%2C%20so%20it%20is%20backwards%20compatible%20with%20older%20versions%20of%20Excel.%20It%20will%20show%20in%20the%20screenshot%2C%20but%20not%20if%20you%20open%20an%20Excel%20file.%3C%2FP%3E%3C%2FLINGO-BODY%3E
John_M2205
New Contributor

Hello.

 

I currently have a column of data that I need to select a random number from. I have the correct formula for that. The issue is that I need to exclude the cell that was "picked" in the previous formula from the next result I need to generate (when I drag the formula down).  Here is the formula I am using to generate the random number: =INDEX($B2:$B47,RANDBETWEEN(1,COUNTA($B2:$B47)),1). 

 

Here's an example if this is hard to understand. Cell A1= 3, Cell A2=4, Cell A3=5, Cell A4=6. My formula in B1 chose the number 4. Now, in B2, I need a number that excludes cell A2 (4).  Let's say the next number is 3. Now in B3, I need a formula that generates a random number but excludes both A1 and A2. If anyone can help, please do!

9 Replies

Hello @John_M2205 ,

 

what is the expected result? What does the formula calculate? How does "excluding" look like, compared to not excluding? Maybe you can create a small data sample and upload the workbook here.

Here is my example! The expected result is a return from the S&P 500 from a previous year. Once that return is generated, I do not want it generated again once I drag the formula down. For example, the first number generated from the collection of data (column b) was 1.06. Since that number was generated, I do not want that number to be considered when I generate the next random number. Thanks for your response and I appreciate any feedback!@Ingeborg Hawighorst 

Hi @John_M2205 ,

 

You can do this with a helper table. You basically want to create a list of the 46 row numbers sorted randomly, which you can then feed into the Index function. To do this

 

- create a helper column with the row numbers from 1 to 46 on the same or a different sheet. 

- next to that column enter the =Rand() function and copy down. 

- turn these two columns into an Excel table

- sort the table by the column with the Rand function

- you now have a randomly sorted list of the unique row numbers

- point the Index function to the row number column of the table. 

 

=@INDEX($B$2:$B$47,Table1[@[unique numbers]])

 

Or, if you don't want to use structured referencing, you can use

 

=@INDEX($B$2:$B$47,F2)

 

Every time you sort the helper table, the Rand() function will recalculate, so if you want to shuffle your numbers, just sort the Rand() column. 

2019-08-05_09-17-36.jpg

 

Your original formula counts the number of entries in column B, so you may have a varying number of rows. To cater for that, create as many row numbers in the helper as you will need for your largest possible data set. Then, instead of just the Rand() function, use this formula for the sort column:

 

=IF(COUNT(B:B)>=ROW()-1,RAND(),0)

 

This will generate random numbers only for the row numbers with content and create 0 values for the rest. Now you can sort from large to small to get the random order of the row numbers.

 

 

@John_M2205 

This is probably jumping the gun somewhat because dynamic arrays are only just starting to be released to Office 365 users.  DAs allow the same strategy of generating a set of random numbers and using their sort order that was used by @Ingeborg Hawighorst to be applied directly to the list of values without the use of a helper range or index lookup.

 

= SORTBY( ValueList, RANDARRAY(COUNT(ValueList)) )

I Have one more question. When you say, "sort the table with the Rand function" do you mean the following steps: click on a column, click "sort & filter", click "sort by random number", Sort on "Values". And then what order? I am not sure what you mean when you say to sort by the rand function. Thank you!  @Ingeborg Hawighorst 

@John_M2205 

I think @Ingeborg Hawighorst will have used the RANK function to rank each instance of a random number within the list.  This will generate values 1-46 that are used as the index (row number) parameter within the INDEX function.

 

I am puzzled by the "@" symbol that appears before the INDEX function in the formula bar (blocking an array calculation to return a single value).  Who is it that is using an up-to-the-minute version of Office 365?  

 

[Just checked back; it was Ingeborg's post that included the symbol]

@John_M2205  I meant: Sort the table manually, using the sort command on the column with the RAND function. Sort order does not really matter, unless you use the second technique where you have zero values in the column. In that case, sort descending.

@Peter Bartholomew  Yes, I use Office Insider, which already includes the new Dynamic Array functions. It's sometimes hard to write functions the old way, because things now just work. The backwards compatibility is achieved with the @ symbol that tells a function not to use the new array way of things, so it is backwards compatible with older versions of Excel. It will show in the screenshot, but not if you open an Excel file.

Related Conversations