Allow Get-Random -Count to return duplicates
See original GitHub issueSummary of the new feature/enhancement
Get-Random should be able to return the same result multiple times when the Count parameter is specified.
Today when you run:
1..10 | Get-Random -Count 10
you will always get one of each number from 1-10 but you should be able to get any number multiple times.
This would also fix an issue where you get fewer items than you expected because there weren’t enough inputobjects:
1..10 | Get-Random -Count 20
Proposed technical implementation details (optional)
A switch parameter like -Unique or -AllowDuplicates could be added to control this behavior VS the current one.
I like the “Unique” parameter name, but having a switch that is enabled by default (for backwards compatibility) is a bad user experience IMO.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Insert Delete GetRandom O(1) - Duplicates allowed
int getRandom() Returns a random element from the current multiset of elements. The probability of each element being returned is linearly related to...
Read more >c# - Random number generator with no duplicates
Basically I'm creating a program to randomly generate 6 unique lottery numbers so there is no duplicates in the same line, here is...
Read more >Design a data structure that supports insert, delete ...
getRandom (): Returns any value present in the stream randomly. ... we will design a data structure that can handle duplicate elements also....
Read more >Insert Delete GetRandom O(1) - Duplicates Allowed - YouTube
This video explains the problem Insert Delete GetRandom O(1) - Duplicates Allowed from Leetcode, provides an easy to understand algorithm ...
Read more >Insert Delete GetRandom O(1) - Duplicates allowed in C++
Suppose, we want to make a data structure, that supports some operations, these operations must be preformed in O(1) amount of time. So...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@MartinGC94
I think this working as designed at present.
If you want numbers between 1 and 10 with possible repetitions, then
does the trick. But if you want the first n of a randomly ordered set this returns it .
And works for any object.
Note that this
Get-Random -InputObject @('Heads','Tails') -Count 10
Stops after two. There is no error if count > set_size.@237dmitry 's example with 2* actually needs to be count * so (e.g) the verb “Set” can appear 10 times.
But if you want the option to select items in the set more than once these are better
or
A switch
-allowduplicates
which only applies when-inputObject
is specified would clean this up.It’s true; I learned the term in a college class (don’t remember if it was a straight-up CS course or a required-for-CS stats course).
The etymology of the term is simple, though: suppose you are drawing some colored marbles out of a sack, one at a time. From this you can pose the question “what is the chance you will draw N of the same color in a row” or somesuch. Naturally this depends on the numbers of different-colored marbles in the sack, but it also depends on your drawing behavior: after drawing a marble, do you put it back into the sack before drawing the next one? (i.e. do you replace it?)
Hence “selection WITH replacement” (putting the marbles back as you draw them), or “selection WITHOUT replacement” (don’t put any marbles back in).
If I saw an
-AllowDuplicates
parameter here, I might also be confused. I could wonder if it meant that if there are duplicates in the input, then without this param I will get an error? (because otherwise they are not “allowed”)So I feel like the better tradeoff would be to use the more precise technical term. Yes, people might have to read the doc if they haven’t had a stats course; but better to raise the general educational level of everyone than keep the confusing term for everyone (honestly, everyone would probably have to read the doc to understand what
-AllowDuplicates
would actually mean for this cmdlet, too).