对单个int使用np.random是不好的做法吗? [英] Is using np.random bad practice for a single int?

查看:51
本文介绍了对单个int使用np.random是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是对答案的评论我最近发布的信息,

Following from a comment on an answer I recently posted,

您使用numpy.random而不是标准random的任何原因模块?

Any reason you are using numpy.random instead of the standard random module?

@Gulzar有任何理由不这样做吗?"-是:ModuleNotFoundError:否模块名为"numpy".要求使用外部库进行操作是不明智的做法使用标准库可以很好地完成某些任务.实际上,标准库会使程序更短(导入random; random.randint(low_limit,high_limit + 1)).

@Gulzar "Any reason not to?" - Yes: ModuleNotFoundError: No module named 'numpy'. It's bad practice to require an external library for something that can be done equally well using the standard library. Actually, the standard library would make the program slightly shorter (import random; random.randint(low_limit, high_limit + 1)).

我想更好地理解为什么使用 np.random 比标准随机数差很多?

I would like to better understand why using np.random is so much worse than just the standard random?

在这种情况下,我更喜欢 np.random 的原因:

Reasons I preferred np.random in this case:

  1. 我的代码库基于numpy,从未遇到过麻烦.可以 pip安装它.
  2. 这是非常普遍的.

当人们可以使用标准时,使用 numpy 真的有什么错误吗?由于什么时候使用外部库是一种不好的做法(在发布此问题时有4项投票)?

Is there really something wrong with using numpy when one could use the standard? Since when is using external libraries bad practice (with 4 upvotes at the moment of posting this question)?

我认为示例比标准示例差:

  1. 不可读
  2. 很难合并到您的代码中

也许我应该在该列表中添加一些项目?

Maybe I should add some items to that list?

我希望以后避免不良做法:)

I would like to avoid bad practices in the future :)

我希望我提出的问题足够具体,不要被认为是基于观点的.
如果问题还不够明确,请提出改进​​建议而不是结束.

推荐答案

在已导入 numpy ipython 会话中:

In [1]: import random
In [2]: random.randint(0,100)
Out[2]: 89
In [3]: timeit random.randint(0,100)
1.5 µs ± 2.05 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [4]: timeit np.random.randint(0,100)
5.46 µs ± 121 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [5]: timeit np.random.randint(0,100,1)[0]
23.3 µs ± 1.32 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

这篇关于对单个int使用np.random是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆