python习题——随机整数生成类

随机整数生成类
可以先设定一批生成数字的个数,可设定指定生成的数值的范围

1.普通类实现

import random
import random


class RandomGen:
    def __init__(self, start=1, stop=100, count=100):
        self.start = start
        self.stop = stop
        self.count = count
        
    def generate(self):
        return [random.randint(self.start, self.stop) for i in range(self.count)]
    
    
print(RandomGen().generate())
>>> [random.randint(0, 10) for i in range(50)]
[8, 7, 10, 9, 6, 4, 2, 4, 3, 10, 10, 10, 1, 3, 4, 3, 0, 3, 8, 5, 4, 1, 10, 3, 2, 10, 4, 8, 8, 4, 4, 4, 6, 2, 2, 9, 8, 7, 9, 6, 9, 2, 8, 3, 9, 3, 8, 0, 0, 2]
>>> len([random.randint(0, 10) for i in range(50)])
50
>>> R=RandomGen()
>>> R.generate()
[18, 18, 13, 59, 15, 20, 67, 79, 23, 95, 68, 74, 3, 61, 77, 1, 98, 33, 83, 54, 22, 62, 56, 13, 11, 13, 39, 92, 2, 56, 41, 25, 13, 32, 84, 26, 16, 65, 78, 27, 92, 39, 90, 75, 59, 16, 76, 29, 39, 68, 28, 16, 7, 82, 17, 79, 45, 59, 36, 62, 41, 49, 96, 27, 12, 25, 28, 43, 74, 30, 67, 47, 62, 21, 92, 75, 100, 61, 40, 61, 73, 10, 39, 42, 12, 5, 69, 13, 94, 77, 91, 16, 75, 20, 50, 78, 40, 89, 18, 64]

2.作为工具类来实现,提供类方法

import random


class RandomGen:
    @classmethod
    def generate(self, start=1, stop=100, count=10):
        return [random.randint(start, stop) for i in range(count)]


print(RandomGen().generate())
  1. 生成器实现1
import random


class RandomGen:
    def __init__(self, start=1, stop=100, count=100):
        self.start = start
        self.stop = stop
        self.count = count
        self._gen = self._generate()

    def _generate(self):
        while True:
            yield random.randint(self.start, self.stop)
            
    def generate(self):
        return [next(self._gen) for i in range(self.count)]

print(RandomGen().generate())
  1. 生成器实现2
import random


class RandomGen:
    def __init__(self, start=1, stop=100, count=100):
        self.start = start
        self.stop = stop
        self.count = count
        self._gen = self._generate()

    def _generate(self):
        while True:
            yield random.randint(self.start, self.stop)

    def generate(self):
        yield from (next(self._gen) for i in range(self.count))


print(list(RandomGen().generate()))
  1. 生成器实现3
import random


class RandomGen:
    def __init__(self, start=1, stop=100, count=100):
        self.start = start
        self.stop = stop
        self.count = count
        self._gen = self._generate()

    def _generate(self):
        while True:
            yield random.randint(self.start, self.stop)

    def generate(self, count=0):  # 可以在后期生产数据时控制个数
        count = self.count if count <= 0 else count
        return [next(self._gen) for i in range(self.count)]


print(RandomGen().generate(5))
print(RandomGen().generate())
  1. 生成器实现4
import random


class RandomGen:
    def __init__(self, start=1, stop=100, count=100):
        self.start = start
        self.stop = stop
        self._count = count
        self._gen = self._generate()

    def _generate(self):
        while True:  # 一次yield 一批
            yield [random.randint(self.start, self.stop) for _ in range(self._count)]

    def generate(self, count=0):  # 可以在后期生产数据时控制个数
        if count > 10:
            self._count = count
        return next(self._gen)


print(RandomGen().generate(5))
print(RandomGen().generate())
  1. 生成器实现5
import random


class RandomGen:
    def __init__(self, start=1, stop=100, count=100):
        self.start = start
        self.stop = stop
        self._count = count
        self._gen = self._generate()

    def _generate(self):
        while True:  # 一次yield 一批
            yield [random.randint(self.start, self.stop) for _ in range(self._count)]

    def generate(self):  # 可以在后期生产数据时控制个数
        return next(self._gen)

    @property
    def count(self):
        return self._count
        
    @count.setter
    def count(self, count):
        self._count = count
        
r = RandomGen()
print(r.count)
print(r.generate())
r.count = 3
print(r.generate())

转自:https://www.cnblogs.com/duanming/p/11830283.html

你可能感兴趣的:(python习题——随机整数生成类)