推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
LHZo
V2EX  ›  Python

Python 写一个算法

  •  
  •   LHZo · Oct 20, 2018 · 4134 views
    This topic created in 2760 days ago, the information mentioned may be changed or developed.

    需求: 在 1~10000000 范围内, 取 500000 个随机数,保证不重复, 每次取出数据缩小范围优化速度(不可使用 random.sample )

    有没有比我写的还简单的....

    def demo():
    
        dic = {i:i for i in range(1,10000001)}
    
        ls = []
        for i in dic.values():
            ls.append(dic.values())
    
            if len(ls) == 500000:
                break
        print(len(ls))
    
    
    import timeit
    t = timeit.Timer('demo()', 'from __main__ import demo')
    
    print(t.timeit(1))
    
    17 replies    2018-10-21 10:26:32 +08:00
    LHZo
        1
    LHZo  
    OP
       Oct 20, 2018
    突然想起来 字典的无序 好像不符合 随机数这个点.......... 这个算法是错的= =!!!!求大佬
    zhaoxiaowen123
        2
    zhaoxiaowen123  
       Oct 20, 2018
    i for i in range(1,10000001) 是遍历,不是随机给, 你是不是想 random.randint
    ericls
        3
    ericls  
       Oct 20, 2018 via iPhone
    Generator
    renyijiu
        4
    renyijiu  
       Oct 20, 2018
    看不懂,你这 for i in dic.values() , i 又没有使用,什么逻辑?
    renyijiu
        5
    renyijiu  
       Oct 20, 2018
    接上面,你这数组不会炸吗?
    alixali
        6
    alixali  
       Oct 20, 2018
    https://www.cnblogs.com/forget406/p/5294143.html

    好像这个是伪随机数产生的算法,和你这个有点不一样
    Trim21
        7
    Trim21  
       Oct 20, 2018 via Android
    Python36 以上的 dict 已经有了,这样行不通吧
    Trim21
        8
    Trim21  
       Oct 20, 2018 via Android
    @Trim21 有序*
    FanWall
        9
    FanWall  
       Oct 20, 2018   ❤️ 1
    dic=[i for i in range(1,10000001)]
    random.shuffle(dic)
    ls=dic[:500000]
    rabbbit
        10
    rabbbit  
       Oct 20, 2018
    import random
    arr = set()
    while len(arr) < 500000:
    arr.add(random.randint(1, 10000001))
    freemoon
        11
    freemoon  
       Oct 20, 2018
    随机不是那么容易实现的,字典也只是看似无序
    punderson
        12
    punderson  
       Oct 20, 2018
    这代码好像 append 也不对吧,应该是 append(i) 吧,而不是 append(dic.values())
    ys759206502
        13
    ys759206502  
       Oct 20, 2018
    是不可使用 random.sample 还是不可使用 random,如果是前者的话研究研究 random 别的方法,后者的话……自己写个伪随机?
    xpresslink
        14
    xpresslink  
       Oct 20, 2018
    楼主的基础知识也太差了些。字典的无序不是随机,那个键值做 hash 运算后是固定规律可寻的。
    不知道楼主的随机数用途是什么,如果只是玩玩那么用 C 的 random 函数( python 的随机函数本质上是和 C 一样的)产生的伪随机数凑合用是可以的。但是大样本下这是不能做正式生产用途的,是可以找到规律的。

    真随机数靠程序是永远无法实现的,真正的业务系统都是用有认证的硬件随机数发生器的,那是用电路热噪声,布朗运动,量子效应,放射性衰变等做随机种子。
    如果不想投资硬件,只能是找一台磁盘比较忙的 LINUX 服务器,从 /dev/random 读字节流,这个是真随机数,只是性能不高。
    shm7
        15
    shm7  
       Oct 20, 2018
    Mersenne Twister, the mostly widly used pseudo-random-number-generator

    随机算法,你先看看这个基础的 wiki,你再想想你这个想法的实际场景,以及新的算法。反正研究是这么来的。
    ltoddy
        16
    ltoddy  
       Oct 21, 2018
    那个啥, 我想说, 什么叫随机数, 计算机里没有真正的随机数, 都是伪随机数, 那么好了:

    ```
    for i in range(500000): yield hash(i * 0.1) % 10000000
    ```

    随机数算法嘛, 还是用梅森旋转算法的好. 但是代价不低.
    ltoddy
        17
    ltoddy  
       Oct 21, 2018
    @LHZo Python 3.6 的时候, cpython 的 dict 效仿了 pypy 的实现方案, 他会跟踪你 dict 添加元素的顺序.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1081 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 52ms · UTC 22:54 · PVG 06:54 · LAX 15:54 · JFK 18:54
    ♥ Do have faith in what you're doing.