Python Forum
How to Generate and Print An Array with Random Numbers in Python in the
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Generate and Print An Array with Random Numbers in Python in the
#11
I also disbelieve any "theory" that random.sample returns duplicates (if there are none in the population given to random.sample). I bet I know the exact algorithm random.sample is using, and it's not possible for that algorithm to return duplicates. The only way it could is some sort of data corruption, in which case there is no possible way to guarantee no duplicates.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#12
Let me make a wild guess that the issue is in combinatorics. I think.
The objection may be that the probabilities of the numbers being selected varies as the selections are made. Example - select 10 numbers in the range 1 to 20. The first number selected had a probability of being selected of 1 in 20. However, when you get to the last number selected, that number had a probability of being selected of 1 in 11. So, the probabilities vary.
For this problem where it appears you want 25 unique out of [1:100) you could grab 25 numbers, check to see if duplicates, if so then reject that panel and pull another 25 numbers. That would keep the probabilities equal.
Reply
#13
It doesn't matter. There is a known algorithm for making a random permutation of a list where all possible permutations are selected with the same probability (assuming a long enough period on your PRNG):

def shuffle(seq):
    for i in range(len(seq)):
        swap = random.randrange(i, len(seq))
        seq[i], seq[swap] = seq[swap], seq[i]
You do that and take the first n numbers, and you have a random sample of size n. You can even stop it after n iterations, since the rest of them don't matter. And since it is all done by swapping two items in the list, there can never be a duplicate unless there was a duplicate in the list to start.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#14
Agree. I think the objection was to the use of random.sample() in a sequential process. Probabilities do change in that setting.
Reply
#15
The probabilities change because you're not selecting duplicates, so the objection still makes no sense.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,369 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  random interger array question rpang 3 1,779 Nov-05-2022, 12:31 PM
Last Post: deanhystad
  Convert list of numbers to string of numbers kam_uk 5 2,935 Nov-21-2020, 03:10 PM
Last Post: deanhystad
  Getting largest indices of array less than or equal to an array of numbers schniefen 5 2,582 Nov-02-2020, 08:14 PM
Last Post: schniefen
  Scaling random numbers within specific range schniefen 4 3,135 Oct-28-2019, 11:22 AM
Last Post: jefsummers
  python homework print the sum of a range of numbers from x to y kirito85 3 3,231 Oct-28-2018, 08:56 AM
Last Post: kirito85
  10x10 Array of Prime Numbers smfox2 1 2,501 Sep-03-2018, 12:36 AM
Last Post: ichabod801
  random numbers and stats cliffhop23 2 6,852 Feb-22-2018, 09:16 PM
Last Post: glidecode
  Regular Expressions in Files (find all phone numbers and credit card numbers) Amirsalar 2 4,053 Dec-05-2017, 09:48 AM
Last Post: DeaD_EyE
  NEED HELP Pseudo-Random Numbers Kongurinn 9 5,342 Oct-23-2017, 04:17 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020