Bottom Page

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

Quote
#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.
Quote
#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

Quote
#14
Agree. I think the objection was to the use of random.sample() in a sequential process. Probabilities do change in that setting.
Quote
#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

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  python homework print the sum of a range of numbers from x to y kirito85 3 596 Oct-28-2018, 08:56 AM
Last Post: kirito85
  10x10 Array of Prime Numbers smfox2 1 624 Sep-03-2018, 12:36 AM
Last Post: ichabod801
  random numbers and stats cliffhop23 2 1,643 Feb-22-2018, 09:16 PM
Last Post: glidecode
  Regular Expressions in Files (find all phone numbers and credit card numbers) Amirsalar 2 1,375 Dec-05-2017, 09:48 AM
Last Post: DeaD_EyE
  NEED HELP Pseudo-Random Numbers Kongurinn 9 1,393 Oct-23-2017, 04:17 AM
Last Post: Skaperen
  List with Random Numbers AnjyilLee 5 2,412 Oct-14-2017, 09:22 PM
Last Post: buran
  Python Exercise: Generate a two-dimensional array smbx33 4 3,982 Apr-22-2017, 11:51 PM
Last Post: smbx33

Forum Jump:


Users browsing this thread: 1 Guest(s)