Generate unique random integers

Generate unique random integers

Generating unique random integers is an important task in various domains. Examples include data sampling, simulations, and even lottery picks. Products have come a long way from when one had to write a fair amount of code to do the needful. Below are two solutions, one in MS Excel and the other in Python.

Suppose we want 6 random numbers from the range 1 to 54. Here the population is the list of 54 integers between 1 and 54 and the sample size is 6.

The intuitive way to do pick random numbers is to repeatedly generate numbers between 1 and 54 and check that it is not already selected . However, this gets extremely wasteful as the sample size approaches the population size. Imagine the extreme case where we want to pick 54 random numbers between 1 and 54. There's nothing random about the last pick. So, repeatedly generating a number between 1 and 54 and checking if it is already selected is...well, wasteful.

It's easier to sort the list of 54 numbers in random order and pick the top 6.

In Excel, the below function does the needful:

=LAMBDA(_Min,_Max,_NbrVals,
? ? LET(_Range,_Max-_Min+1,
? ? ? ? INDEX(SORTBY(SEQUENCE(_Range,,_Min),RANDARRAY(_Range)),SEQUENCE(_NbrVals))
? ? ? ? ))        
No alt text provided for this image

How does it work?

  1. The SEQUENCE(_Range,,_Min) generates the integers between _Min and _Max. That would be 1 and 54 in the above example.
  2. The RANDARRAY(_Range) generates an array of random numbers of the same size as above - 54 elements in the above example.
  3. The SORTBY sorts the numbers from 1 in the order of the random numbers. This essentially randomizes the list of integers.
  4. The SEQUENCE(_NbrVals) generates the numbers 1,2,...,_NbrVals - this would be 1,2,..,6 in the above example.
  5. The INDEX() function picks the first _NbrVals values (6 in the above example - see 4 above) from the *randomized* list of integers from SORTBY (from 3 above).

Of course, even better would be if we had a function that did the needful for us. And, in Python, random.sample does just that.


import random
print(random.sample(range(1, 54+1),k=6))

[47, 20, 54, 38, 41, 12]        

For more on this function see random — Generate pseudo-random numbers — Python 3.11.1 documentation

Do you have occasion to generate unique random integers? How do you do so?

My previous post: MS Excel: Goal Seek or Algebra | LinkedIn

Rick Rothstein

Microsoft MVP (Excel)

2 年

Here is a LinkedIn article I wrote which includes a VBA function that iterates its loop only as many times as unique random numbers requested (for example, it only loops 6 times to produce 6 unique random numbers). Note the first 2/3s or so of code is devoted to "house-keeping" chores... the juicy part of the code is in the last For..Next loop... www.dhirubhai.net/pulse/get-random-number-unique-items-from-array-rick-rothstein/

回复

要查看或添加评论,请登录

Tushar Mehta的更多文章

  • MS Excel: How to develop a recursive function

    MS Excel: How to develop a recursive function

    Introduction A recursive function returns a value that depends on the value that the function returns for another set…

    2 条评论
  • Phone Number Region Lookup

    Phone Number Region Lookup

    Introduction We call people using phone numbers without giving much thought to what the number represents. Each number…

    1 条评论
  • MS Excel - Graph a Spill Range

    MS Excel - Graph a Spill Range

    Introduction to Dynamic Array Formulas and Spill Ranges Microsoft implemented a major improvement in Excel with the…

  • List and count unique values (MS Excel, Python, and SQL)

    List and count unique values (MS Excel, Python, and SQL)

    This is a fairly common requirement that until recently had a convoluted solution in Excel. Given a list of values, how…

    1 条评论
  • Excel Large Number Arithmetic with Python

    Excel Large Number Arithmetic with Python

    Introduction When it comes to numbers, MS Excel follows the IEEE 754 standard (Floating-point arithmetic may give…

  • MS Excel: Recursive functions

    MS Excel: Recursive functions

    In MS Excel: Functions are first-class objects, we saw how a custom Excel function, using LAMBDA, can call another…

    3 条评论
  • MS Excel: Functions are first-class objects

    MS Excel: Functions are first-class objects

    What makes a function a first-class object? This is almost certainly an advanced discussion. And, we will start with a…

  • MS Excel: Goal Seek or Algebra

    MS Excel: Goal Seek or Algebra

    Let's start with a question. I want to pay a business through a payment processor such as PayPal.

    1 条评论
  • Calendar for the New Year

    Calendar for the New Year

    For the New Year..

    1 条评论
  • Validating U.S. Bank Routing Numbers

    Validating U.S. Bank Routing Numbers

    In the U.S.

社区洞察

其他会员也浏览了