Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

random import works? #44375

Closed
msword mannequin opened this issue Dec 23, 2006 · 4 comments
Closed

random import works? #44375

msword mannequin opened this issue Dec 23, 2006 · 4 comments
Labels
extension-modules C modules in the Modules dir

Comments

@msword
Copy link
Mannequin

msword mannequin commented Dec 23, 2006

BPO 1621367
Files
  • random checker.py: Checks if random.choice() is all that random
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2006-12-26.08:54:26.000>
    created_at = <Date 2006-12-23.17:04:36.000>
    labels = ['extension-modules', 'invalid']
    title = 'random import works?'
    updated_at = <Date 2006-12-26.08:54:26.000>
    user = 'https://bugs.python.org/msword'

    bugs.python.org fields:

    activity = <Date 2006-12-26.08:54:26.000>
    actor = 'mark-roberts'
    assignee = 'none'
    closed = True
    closed_date = None
    closer = None
    components = ['Extension Modules']
    creation = <Date 2006-12-23.17:04:36.000>
    creator = 'msword'
    dependencies = []
    files = ['2258']
    hgrepos = []
    issue_num = 1621367
    keywords = []
    message_count = 4.0
    messages = ['30861', '30862', '30863', '30864']
    nosy_count = 3.0
    nosy_names = ['mark-roberts', 'msword', 'dallison']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = None
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue1621367'
    versions = ['Python 2.5']

    @msword
    Copy link
    Mannequin Author

    msword mannequin commented Dec 23, 2006

    I'm just starting working with python, and it seems that random.choice() isn't all that random. After running the program(attached) a few times, each and every time it selects 1 < 2 < 3 < 4 < 5 and I can expect this to happen, meaning it isn't all that random.
    Email: Kentsfx2@gmail.com

    Thanks

    @msword msword mannequin closed this as completed Dec 23, 2006
    @msword msword mannequin added invalid extension-modules C modules in the Modules dir labels Dec 23, 2006
    @msword msword mannequin closed this as completed Dec 23, 2006
    @msword msword mannequin added invalid extension-modules C modules in the Modules dir labels Dec 23, 2006
    @dallison
    Copy link
    Mannequin

    dallison mannequin commented Dec 23, 2006

    I believe the problem is with your test framework and not with random.choice(). The library function random.choice( seq ) selects, using a uniform distribution, one item from the sequence at each call. By the law of large numbers, if you have K items in the sequence, each should be returned K/N times, on average, after N calls. You should expect deviations even for fairly large N. If you want to test the randomess, use a chi-square test to test against the hypothes of uniform random selection with replacement. Of course there are many other statistical properties which ought to be checked, for example, the distribution of runs.

    Consider the program:

    import random
    dist = [0,0,0,0,0]
    for i in range(100000):
    	j = random.choice([0,1,2,3,4])
    	dist[j] += 1
    print dist

    which prints the distribution observed for each choice. With
    100000 tries you'd expect each one to appear (on average) 20000
    time. Running it on my a three times gives:

    [19839, 19871, 19996, 20035, 20259]
    [20043, 19870, 20025, 20109, 19953]
    [19947, 20033, 19970, 20111, 19939]

    @mark-roberts
    Copy link
    Mannequin

    mark-roberts mannequin commented Dec 26, 2006

    This is an issue with the way you have written your initial application. See the attachment that I've supplied to explain what is going wrong in your program. It really boils down to you adding X to each value, while X is steadily increasing in value (1, 2, 3, 4, 5). At one point, you actually add 1 to one value, and add 5 to another (for the same random choice).

    • Mark

    @mark-roberts
    Copy link
    Mannequin

    mark-roberts mannequin commented Dec 26, 2006

    Sorry, it appears that I can't upload a file, or I'm missing the button. Anyway, you can find the file you're lookign for here:

    http://www.pandapocket.com/python/rand2.pys

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    extension-modules C modules in the Modules dir
    Projects
    None yet
    Development

    No branches or pull requests

    0 participants