+ Reply to Thread
Results 1 to 13 of 13

Generate Unique Random lettes 'a' to 'z' for a row ?

  1. #1
    Registered User
    Join Date
    03-06-2020
    Location
    Dallas
    MS-Off Ver
    2016
    Posts
    6

    Generate Unique Random lettes 'a' to 'z' for a row ?

    I am working on a task that requires me to have unique random values from 'a to z'. (it could even be 1 to 26, so, either all numbers or all letters).

    In one row + 26 columns, it would have 1 letter each from a to z, without any letter being repeated. (example image attached).

    Is it possible to do this for 40,000 + rows? without copying the same sequence? i.e. each row, would have it's own random sequence and hence the chances of any two row being similar would be significantly low.

    If anyone can help, it would be very much appreciated.

    Thank you.
    Attached Images Attached Images

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Generate Unique Random lettes 'a' to 'z' for a row ?

    It is impossible without a Macro. A formula cannot handle the no duplicates issue

    You and either Call the Macro Directly or use a user defined function.

    I think that there are 10888869450418400000000000000 combinations

    So it should be simple to Delete duplicate rows. And have 40000 unique rows.
    Last edited by mehmetcik; 03-06-2020 at 10:37 AM.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Registered User
    Join Date
    03-06-2020
    Location
    Dallas
    MS-Off Ver
    2016
    Posts
    6

    Re: Generate Unique Random lettes 'a' to 'z' for a row ?

    I am relatively new to Excel & would not even know how to go about writing a Macro or what it looks like or how short/long it would be.

    If it is possible & not too much trouble for you, would you be able to write one? however, if it is something that you can't or that would take a long time, any idea on how I would go about trying to solve this problem?

    Thanks for the help & guidance, I appreciate it.

  4. #4
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,819

    Re: Generate Unique Random lettes 'a' to 'z' for a row ?

    I'm a little more optimistic than mehmetcik that this can be done without a macro -- unless, of course, you insist that you cannot use helper columns/cells for the task.

    Whatever programming language you choose to do this in, the basic algorithm I would expect to use is often called a "shuffle" -- because the idea is that you want all 26 elements in a random order. In this kind of algorithm, you assign a unique random number (in Excel, you would use the RAND() function) to each of your 26 letters/numbers/elements. Then you sort the list by those random numbers (maybe using the SMALL() or LARGE() functions in Excel). If I were setting something like this up, I would probably:

    1) In A1, enter =RAND(). Copy/paste/fill across to Z1.
    2) In AA1, enter =MATCH(SMALL($A1:$Z1,COLUMN(A1)),$A1:$Z1,0) where
    2a) The SMALL(...) function returns the kth smallest random number where k is determined by the COLUMN() function.
    2b) the MATCH(...) function then returns the column number for that particular random number.

    That should give you the numbers 1 to 26 in random order. Then copy that sequence down as many rows as needed to get that many random shuffles.

    Of course, this approach does not guarantee that you will get 40000 unique sequences. You will then either need to check for duplicate "shuffles" and reject them. Or, if having duplicate sequences is a very real problem/concern, you could build a (painfully long) list of every possible sequence, then "shuffle" that list instead.

    Does that sound like an approach you can use?
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  5. #5
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Generate Unique Random lettes 'a' to 'z' for a row ?

    Please Login or Register  to view this content.

  6. #6
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,410

    Re: Generate Unique Random lettes 'a' to 'z' for a row ?

    Here is a macro that will produce 40,000 guaranteed unique randomized sequences of the letters A thru Z...
    Please Login or Register  to view this content.
    HOW TO INSTALL MACROs
    ------------------------------------
    If you are new to macros, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. To use the macro, go back to the worksheet with your data on it and press ALT+F8, select the macro name (ShuffleAlphabet40000Times) from the list that appears and click the Run button. The macro will execute and perform the action(s) you asked for. If you will need to do this again in this same workbook, and if you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.


    EDIT NOTE: I should add for future viewers of this thread that my code will only work on a PC (it will not work on a Mac).
    Last edited by Rick Rothstein; 03-06-2020 at 05:18 PM.

  7. #7
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: Generate Unique Random lettes 'a' to 'z' for a row ?

    An example of what Mr Shorty was explaining. In Column AA, I concatenated and did a conditional formatting to color duplicates red. I still have the Rand() function on Sheet2 so the sequences will be changing every time the workbook does a calculation. To stop that, Copy >Paste Values on Sheet2. I had set it up for 10000 rows but had to shrink it for file size purposes.
    Attached Files Attached Files
    ChemistB
    My 2?

    substitute commas with semi-colons if your region settings requires
    Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
    If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  8. #8
    Registered User
    Join Date
    03-06-2020
    Location
    Dallas
    MS-Off Ver
    2016
    Posts
    6

    Re: Generate Unique Random lettes 'a' to 'z' for a row ?

    Hello MrShorty

    You are very helpful, however perhaps because of lack of Excel understanding on my part, I wasn't able to grasp all of it.. However, while trying to follow search for right directions & follow suggested steps I noticed the macro from Rick, which worked ..

    Thank you.

  9. #9
    Registered User
    Join Date
    03-06-2020
    Location
    Dallas
    MS-Off Ver
    2016
    Posts
    6

    Re: Generate Unique Random lettes 'a' to 'z' for a row ?

    Hi Mehmetcik,

    I tried this, however, it gave me error 1004 & asked me to 'End' it or 'Debug' ..

  10. #10
    Registered User
    Join Date
    03-06-2020
    Location
    Dallas
    MS-Off Ver
    2016
    Posts
    6

    Re: Generate Unique Random lettes 'a' to 'z' for a row ?

    Hi Rick,

    This macro worked like a charm.. really awesome.., you are a life saver. Thanks

  11. #11
    Registered User
    Join Date
    03-06-2020
    Location
    Dallas
    MS-Off Ver
    2016
    Posts
    6

    Re: Generate Unique Random lettes 'a' to 'z' for a row ?

    Hi ChemistB,

    I looked at the file & it does seem to make sense, however for now, it seems that Rick's Macro did the trick & considering the deadline .. I am going to get it done using that.. Thank you for taking time and provding example, it does mean a lot.

    You all have been wonderful.. after trying several things I was frustrated, however you all made the problem look so easy.. Made my whole weekend much easier ... Thanks again.

  12. #12
    Forum Expert
    Join Date
    07-06-2004
    Location
    Northern California
    MS-Off Ver
    2K, 2003, 2010, O365
    Posts
    1,490

    Re: Generate Unique Random lettes 'a' to 'z' for a row ?

    This could be done directly with rather inefficient formulas.

    A1: =CHAR(RANDBETWEEN(97,122))
    B1: =CHAR(SMALL((COUNTIF($A1:A1,CHAR(ROW($97:$122)))=0)*ROW($97:$122),RANDBETWEEN(COLUMNS($A1:B1),26)))

    Fill B1 right into C1:Z1.

    When you get a version of Excel with spilled formulas and dynamic arrays, this becomes more direct.

    A1: =SORTBY(CHAR(SEQUENCE(1,26,97,1)),RANDARRAY(1,26,0,1),1)

    which will spill into A1:Z1.

  13. #13
    Forum Expert BMV's Avatar
    Join Date
    01-13-2020
    Location
    St-Petersburg, Russia
    MS-Off Ver
    2013-2016
    Posts
    1,329

    Re: Generate Unique Random lettes 'a' to 'z' for a row ?

    hrlngrv , Good job, It could be not uncial string but probability is very low.
    My version was
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 20
    Last Post: 09-27-2019, 10:11 AM
  2. [SOLVED] how to generate random numbers in range 1:20 with unique results
    By jmac1947 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-23-2014, 08:54 AM
  3. How to generate unique random alphanumeric 32 character codes?
    By JussiR in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 02-21-2014, 06:30 AM
  4. Formula to generate unique random numbers?!?
    By aims in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-07-2013, 03:14 PM
  5. Generate unique random alphabets for 35 characters in length
    By promo786 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 11-13-2011, 11:10 PM
  6. Replies: 7
    Last Post: 09-01-2007, 04:29 PM
  7. [SOLVED] generate unique random numbers
    By Stephen Larivee in forum Excel - New Users/Basics
    Replies: 7
    Last Post: 03-28-2006, 08:10 PM

Tags for this Thread

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