Find the most frequently chosen number

Asked 1 weeks ago, Updated 1 weeks ago, 2 views

import numpy as np
import xlwings as xw
import random

for i in range(100):
    F1AA = random.randint(0, 52)
    F1BB = random.randint(0, 52)
    F1CC = random.randint(0, 156)
    F2AA = random.randint(0, 26)
    F2BB = random.randint(0, 52)
    F2CC = random.randint(0, 104)
    F3AA = random.randint(0, 52)
    F3BB = random.randint(0, 52)
    F3CC = random.randint(0, 156)
    P2AA = random.randint(0, 51)
    P2BB = random.randint(0, 68)
    P2CC = random.randint(0, 136)
    P3AA = random.randint(0, 51)
    P3BB = random.randint(0, 51)
    P3CC = random.randint(0, 153)
    S1OO = random.randint(0, 108)
    S2AA = random.randint(0, 6)
    S2BB = random.randint(0, 12)
    S2CC = random.randint(0, 36)
    S3AA = random.randint(0, 6)
    S3BB = random.randint(0, 12)
    S3CC = random.randint(0, 36)

print(f'F1-AA-{F1AA}',f'F1-BB-{F1BB}',f'F1-CC-{F1CC}',f'F2-AA-{F2AA}',f'F2-BB-{F2BB}',f'F2-CC-{F2CC}',f'F3-AA-{F3AA}',f'F3-BB-{F3BB}',f'F3-CC-{F3CC}', f'P2-AA-{P2AA}', f'P2-BB-{P2BB}', f'P2-CC-{P2CC}', f'P3-AA-{P3AA}', f'P3-BB-{P3BB}', f'P3-CC-{P3CC}', f'S1-OO-{S1OO}',f'S2-AA-{S2AA}', f'S2-BB-{S2BB}', f'S2-CC-{S2CC}', f'S3-AA-{S3AA}', f'S3-BB-{S3BB}', }', f'S3-CC-{S3CC}' )

It seems like it's simple coding, but it's getting longer because I'm trying to do something without learning it.

To give you a rough explanation, I want to find the number that comes out the most per code (F1AA, F1BB, etc.) by spinning a lot randomly, but I can only implement it yet.

Is there any way to simplify it or code for finding the numbers that come out a lot here?

I'm sorry I didn't explain too much.


2022-09-20 14:44

1 Answers

I'm not sure. If you have learned probability and statistics, you will know that if you perform random.randomint (0, 52) very often, F1AA will be assigned very evenly with a probability of 1/53 each of the integers from 0 to 52.
For random.randomint(0,156), F1CC would be from 0 to 156. If so, literally finding the "number that came out the most for each code" is a little overshadowed, as long as the law of large numbers is valid.

Looking at the comments again, I guess I just need to get a snapshot of 100 times. Then you can just use the code roughly.

# This makes F1AA a list of 100 random numbers between 0 and 52.
# Perm:
from random import randint
F1AA = [randint(0, 52) for _ in range(100)]

# If you give a list, it's a function that tells you the most frequently appearing element.
# Perm:
def most_frequent(List):
    counter = 0
    num = List[0]     
    for i in List:
        curr_frequency = List.count(i)
        if (curr_frequency> counter):
            counter = curr_frequency
            num = i 
    return num

# Please use a moderate combination.

# By the way.
# If you take a random number from 0 to 52,
# All of those numbers are equally qualified to be the "most likely" numbers.
# That's always the case, whether it's 100 or 1000000.
# Thanks to this mathematical fact, the above code is simplified into the next line.

# A function that obtains the most frequently generated integer when the integer from start to end is randomly selected times
def get_most_frequent(start, end, times) :
    return landint(start, end) # No need to use times...!

2022-09-20 14:44

If you have any answers or tips

© 2022 pinfo. All rights reserved.