[Solved] Generate Random Unique Integers

Discuss the spreadsheet application
Post Reply
MathHawk
Posts: 4
Joined: Sun Aug 05, 2012 6:14 pm

[Solved] Generate Random Unique Integers

Post by MathHawk »

I've been trying for years to generate a list of unique random integers. Many people said it could not be done, others were stumped. Since I finally figured out a way to do it, I thought I would share since I have never found a solution that doesn't use a separate macro or script and involved multiple steps. I was always interested in something that could instantly re-generate without a great deal of effort.

In this example, I used randbetween(), but you could also use a rand() function to generate automatically without manually re-generating with ctrl shift f9.

This can, of course, scale down for smaller lists. I usually only need a few random numbers at a time, but I thought I would scale up to see if my processor could handle it. It did bog down a bit when I re-generate, but it only took a few seconds.

Comments/Questions?
Attachments
Generate Random Unique Numbers.ods
(108.83 KiB) Downloaded 631 times
OpenOffice.org Version 3.4.0 Windows 7 Home Premium SP1 64-bit Operating System
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved] Generate Random Unique Integers

Post by Villeroy »

Much easier:
Create a range of =RAND() as random seed.
Get the rank of each value: =RANK(SeedRange;SeedRange) [entered as array formula with Ctrl+Shift+Enter]
This creates an integer sequence of 1 ... Count(SeedRange). This may be used to pick random but unique values from a third range using the INDEX function.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
MathHawk
Posts: 4
Joined: Sun Aug 05, 2012 6:14 pm

Re: [Solved] Generate Random Unique Integers

Post by MathHawk »

I have tried that in the past but it is unreliable due to ranking issues where two rand() numbers are equal. And it happens more often than you would expect. I wanted something that didn't require checking to make sure there were no errors every time the sets of random numbers were generated.
OpenOffice.org Version 3.4.0 Windows 7 Home Premium SP1 64-bit Operating System
djryanash
Posts: 23
Joined: Sat Apr 01, 2017 6:04 am

Re: [Solved] Generate Random Unique Integers

Post by djryanash »

MathHawk wrote:I've been trying for years to generate a list of unique random integers. Many people said it could not be done, others were stumped. Since I finally figured out a way to do it, I thought I would share since I have never found a solution that doesn't use a separate macro or script and involved multiple steps. I was always interested in something that could instantly re-generate without a great deal of effort.

In this example, I used randbetween(), but you could also use a rand() function to generate automatically without manually re-generating with ctrl shift f9.

This can, of course, scale down for smaller lists. I usually only need a few random numbers at a time, but I thought I would scale up to see if my processor could handle it. It did bog down a bit when I re-generate, but it only took a few seconds.

Comments/Questions?
Thanks for this - it's awesome!!!

Can you briefly explain how it works? It's a bit beyond my ken.
OpenOffice 3.3.0 on MacOS 10.11.4
User avatar
MrProgrammer
Moderator
Posts: 4908
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: [Solved] Generate Random Unique Integers

Post by MrProgrammer »

MathHawk wrote:I've been trying for years to generate a list of unique random integers.
[Tutorial] Randomization in Calc, topic C
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.6.3, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
Post Reply