读取文件并生成随机数的程序

Program to read a file and generate a random number

提问人:Akshat Gaur 提问时间:11/3/2023 更新时间:11/3/2023 访问量:39

问:

让我先解释一下问题。下面显示了从具有一本书的文本文件生成随机数的过程。

* **Seed Initialization**: The PRNG (Pseudorandom Number Generator) is initialized with a given seed. This dictates the starting position within the text. For instance, with a seed of 1,000, the process would start from the 1,000th character in the text.

 

* **Pair Formation**:

  * To generate pairs of letters, start at the initial seed position and move forward by a predetermined step (e.g., 100 characters). This ensures the letters in each pair are independent of one another.

  * After picking the first letter, skip the step interval to select the second letter, forming your first pair.

* If both letters in a pair are same , the pair is discarded and not used in the process.

  * Continue this process to create subsequent pairs. For our purpose, this is done 64 times, resulting in 32 letter pairs.

 

* **Binary Representation**:

  * For each of the 32 pairs, determine the relationship between the letters based on their ASCII values.

  * If the first letter has a greater ASCII value than the second, assign a "1" to a new column for that pair. Otherwise, assign a "0". This assignment is based on the premise that there's a 50% chance for either scenario.

 

* **Probability Assignment**:

  * Each pair is then given a decreasing probability value. The first pair gets a probability of 1/2, the second pair 1/4, the third pair 1/8, and so on. This pattern continues for all pairs.

 

* **Random Number Generation**:

  * Utilize the assigned probabilities and the binary representation (1 or 0) from the earlier step to compute a pseudorandom number. This can be achieved using the formula:

 



 

This method effectively utilizes the text to produce pseudorandom numbers based on the initialized seed and predetermined step value.

To assist with understanding and visualization, these two attached pictures can help complement this description.

这是我的代码。我的主要问题是当 char2 为空字符串时,种子重置为 1000,这可能会导致随机数重复。我尝试使用继续,但它变成了一个无限循环。我希望在那部分得到一些帮助。如果我不这样做,我会得到一个类型错误 if 语句。

from statistics import mean


class WarAndPeacePseudoRandomNumberGenerator:
    def __init__(self,seed=1000,step = 100):

        self.seed = seed
        self.step = step
        self.fileName = "war-and-peace.txt"
        self.file = open(self.fileName, 'r')    # open a file for reading
        self.file.seek(0)                       # start from 0
        
    def readCharacters(self,seed,step):
        #print(self.seed)
        self.file.seek(self.seed)
        char1 = self.file.read(1)
        self.file.seek(self.seed+self.step)
        char2 = self.file.read(1)
        return char1, char2

    def generateList(self):
        binarylist = []
        pairs_generated = 0
        while pairs_generated < 32:
            char1, char2 = self.readCharacters(self.seed, self.step)
            if not char2:
                # If char2 is empty (end of file), reset the seed and step to start over
                self.seed = 1000
                self.step += 100
                continue
            if char1 == char2:
                self.step += 100  # Increase the step by 100 places
                self.file.seek(self.seed + self.step)
                char2 = self.file.read(1)  # Read char2 again with the updated step
                continue
            if ord(char1) > ord(char2):
                binarylist.append(1)
            else:
                binarylist.append(0)

            pairs_generated += 1
            self.step += 100
            self.seed = self.seed + self.step
        return binarylist
    
    def closeFile(self):
        if self.file is not None:
            self.file.close()
    
    def random(self):
        randomNumber = 0.0
        probDenom= 2
        binaryList = self.generateList()
        for i in range (len(binaryList)):
            randomNumber = randomNumber + binaryList[i] * (1/probDenom)
            probDenom = probDenom * 2
        return randomNumber

def main():
    randomNumberList = []
    prng2 = WarAndPeacePseudoRandomNumberGenerator(12345)   # With seed
    r1 = prng2.random()                                     # Random number 1
    r2 = prng2.random()                                     # Random number 2
    prng2.closeFile()                                       # Closing the file

    prng = WarAndPeacePseudoRandomNumberGenerator()         # Without seed value
    for i in range(10):                                  # Generating 10000 random numbers
        r = prng.random()                                   # 
        randomNumberList.append(r)                          # Appending the random number obtained to the list
    prng.closeFile()                                        # Closing the file

    median = mean(randomNumberList)
    minimum = min(randomNumberList)
    maximum = max(randomNumberList)

    print("Random numbers generated with seed 12345: {} {}".format(r1, r2))
    print("\nAfter generating 10000 random numbers")
    print("\nMedian: {} ".format(median))
    print("\nMinimum: {} ".format(minimum))
    print("\nMaximum: {} ".format(maximum))
    print("random num: {}".format(randomNumberList[1:5])) # testing

if __name__ == '__main__':
    main()

python 随机 字符 类型错误

评论

0赞 rossum 11/3/2023
您可以使用当前日期/时间来替换空字符串,或者如果它们离得太近,则可以附加一个计数器。
0赞 SmellyCat 11/3/2023
您希望在什么情况下评估为真?它只是文件的末尾吗?您可以使用更精确的检查来验证您是否已到达文件末尾吗?if not char2if len(char2) < 1
0赞 SmellyCat 11/3/2023
您可以使用该模块生成实际的随机数而不是硬编码为 1000 和 100 吗? 具有函数和 ,它们都采用最小和最大整数值,并返回这些范围内的随机数。randomrandomrandintrandrange
0赞 Akshat Gaur 11/4/2023
我试过使用 len(char2),它现在正在工作。

答: 暂无答案