在本文中,我们将带你了解Python:为什么`random.randint在这篇文章中,我们将为您详细介绍Python:为什么`random.randint的方方面面,并解答a,b`返回一个包含`b`
在本文中,我们将带你了解Python:为什么`random.randint在这篇文章中,我们将为您详细介绍Python:为什么`random.randint的方方面面,并解答a,b`返回一个包含`b`的范围?常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的*#【Python】【基础知识】【模块】【random】【使用random创造一个随机数】、angularjs – 为什么ng-transclude的范围不是它的指令范围的子 – 如果指令有一个孤立的范围?、Java Math.random()与Random.nextInt(int)、Matlab VS Python-eig(A,B)VS sc.linalg.eig(A,B)。
本文目录一览:- Python:为什么`random.randint(a,b)`返回一个包含`b`的范围?(python中return为什么只能返回一个值)
- *#【Python】【基础知识】【模块】【random】【使用random创造一个随机数】
- angularjs – 为什么ng-transclude的范围不是它的指令范围的子 – 如果指令有一个孤立的范围?
- Java Math.random()与Random.nextInt(int)
- Matlab VS Python-eig(A,B)VS sc.linalg.eig(A,B)
Python:为什么`random.randint(a,b)`返回一个包含`b`的范围?(python中return为什么只能返回一个值)
在我看来,总是random.randint(a, b)
返回范围内的整数[a, b]
,而不是[a, b-1]
like
,这总是很奇怪range(...)
。
这种明显的不一致有什么原因吗?
答案1
小编典典我试图通过研究一些旧资料来弄清这个问题。我 怀疑
这randint
是在Python的长整数之前实现的:这意味着,如果您想要一个包含在内的随机数,INT_MAX
则需要调用random.randrange(0,INT_MAX + 1)
该函数,该函数将溢出并导致(0, 0)
或的自变量(0, INT_MIN)
。
但是,回首Python
1.5.2源代码,Lib/whrandom.py
我们看到:
## Get a random integer in the range [a, b] including both end points.# (Deprecated; use randrange below.)#def randint(self, a, b): return self.randrange(a, b+1)
whrandom.randint
继续在被弃用2.0,2.1,2.2,以及2.3
;
但random.randint
在2.1中已标记为已弃用,尽管在2.2中已不再标记为已弃用。
此外,random.py
从2.1版开始,第一个在random.randint
docstring中需要注意的内容:
def randrange(self, start, stop=None, step=1, int=int, default=None): """Choose a random item from range(start, stop[, step]). This fixes the problem with randint() which includes the endpoint; in Python this is usually not what you want. Do not supply the ''int'' and ''default'' arguments. """
早于该版本的唯一可用源是0.9.1源,据我所知,randint
当时尚未实现。
因此,我得出结论,此时,只有Guido本人才知道randint
包含 端点的理由。给定Python
2.1中的文档字符串,听起来原因可能是一个简单的错误。
*#【Python】【基础知识】【模块】【random】【使用random创造一个随机数】
Random介绍:
输出随机数。
快照:
#!/usr/bin/python # -*- coding: UTF-8 -*- import random #生成 10 到 20 之间的随机数 print(random.uniform(10,20)) #输出1-20间的随机数(包含整数、小数) print(random.random()) #输出0-1间的随机数 print(random.randint(10,20)) #输出10-20间的随机整数 print(random.choice([10,11,12,13,14,15])) #随机输出序列内的一个元素 print(random.choice([x for x in range(1,100)])) #输出1-99间的随机数
random模块的方法有:
>>> dir(random) [‘BPF‘,‘LOG4‘,‘NV_MAGICCONST‘,‘RECIP_BPF‘,‘Random‘,‘SG_MAGICCONST‘,‘SystemRandom‘,‘TWOPI‘,‘_BuiltinMethodType‘,‘_MethodType‘,‘_Sequence‘,‘_Set‘,‘__all__‘,‘__builtins__‘,‘__cached__‘,‘__doc__‘,‘__file__‘,‘__loader__‘,‘__name__‘,‘__package__‘,‘__spec__‘,‘_acos‘,‘_bisect‘,‘_ceil‘,‘_cos‘,‘_e‘,‘_exp‘,‘_inst‘,‘_itertools‘,‘_log‘,‘_os‘,‘_pi‘,‘_random‘,‘_sha512‘,‘_sin‘,‘_sqrt‘,‘_test‘,‘_test_generator‘,‘_urandom‘,‘_warn‘,‘betavariate‘,‘choice‘,‘choices‘,‘expovariate‘,‘gammavariate‘,‘gauss‘,‘getrandbits‘,‘getstate‘,‘lognormvariate‘,‘normalvariate‘,‘paretovariate‘,‘randint‘,‘random‘,‘randrange‘,‘sample‘,‘seed‘,‘setstate‘,‘shuffle‘,‘triangular‘,‘uniform‘,‘vonmisesvariate‘,‘weibullvariate‘]
uniform 方法的使用:
>>> dir(random.uniform) [‘__call__‘,‘__class__‘,‘__delattr__‘,‘__dir__‘,‘__eq__‘,‘__format__‘,‘__func__‘,‘__ge__‘,‘__get__‘,‘__getattribute__‘,‘__gt__‘,‘__hash__‘,‘__init__‘,‘__init_subclass__‘,‘__le__‘,‘__lt__‘,‘__ne__‘,‘__new__‘,‘__reduce__‘,‘__reduce_ex__‘,‘__repr__‘,‘__self__‘,‘__setattr__‘,‘__sizeof__‘,‘__str__‘,‘__subclasshook__‘] >>> help(random.uniform) Help on method uniform in module random: uniform(a,b) method of random.Random instance Get a random number in the range [a,b) or [a,b] depending on rounding.
翻译:关于random模块的uniform方法的一些帮助;
random的uniform(a,b) 方法。
random例子:
在[a,b) 或[a,b] 范围内,根据四舍五入,获取一个随机数。
原题:
使用random创建一个随机数;
原题给出的解答:
#!/usr/bin/python # -*- coding: UTF-8 -*- import random #生成 10 到 20 之间的随机数 print(random.uniform(10,20))
输出的结果:
结果精确到小数点后14位;
choice方法的使用
random.choice(seq)
其参数为序列,从序列中随机获取一个随机元素;
示例:
>>> random.choice([1,2,3,4,5,6,7,3]) 2 >>> random.choice([1,3]) 5 >>> random.choice([1,3]) 4 >>> random.choice([1,3]) 3 >>> random.choice([1,3]) 6 >>> random.choice([1,3]) 1 >>> random.choice([1,3]) 1 >>> random.choice("test") ‘t‘ >>> random.choice("test") ‘t‘ >>> random.choice("test") ‘t‘ >>> random.choice("test") ‘e‘ >>> random.choice("test") ‘t‘ >>> random.choice("test") ‘s‘ >>> random.choice("test") ‘e‘
————————(我是分割线)————————
参考:
1. RUNOOB.COM:https://www.runoob.com/python/python-exercise-example50.html
2.Python DOC
备注:
初次编辑时间:2019年10月7日16:12:06
环境:Windows 7 / Python 3.7.2
angularjs – 为什么ng-transclude的范围不是它的指令范围的子 – 如果指令有一个孤立的范围?
Scope 004 <-- scope of the body Scope 005 <-- scope of directive container1 Scope 006 <-- scope of the ng-transclude
我期望:
Scope 004 <-- scope of the body Scope 005 <-- scope of the directive Scope 006 <-- scope of the ng-transclude
如果同一个指令有一个共享作用域而不是一个孤立的作用域,我得到预期的结果。
这使我有一个问题,因为如果被转录的内容包含另一个指令(component1)与一个孤立的范围,我得到:
Scope 004 <-- scope of the body Scope 005 <-- scope of the directive Scope 006 <-- scope of the ng-transclude Scope 007 <-- scope of directive component1
我想使用像这样的指令:
<container1> <component1 data="objectExposedInContainer1"/> </container1>
但是这不工作,在component1内部,$ scope.data是未定义的,因为objectExposedInContainer1不在正确的范围。
我有两个问题:
>为什么ng-transclude的范围不是它的指令范围的子元素,如果指令有一个孤立的范围?这是一个错误?
>如果它不是一个错误,容器指令如何传递数据到它的内容,如果不是通过设置属性,如我试过。
这里是一个不工作的示例:http://plnkr.co/edit/NDmJiRzTF9e5gw8Buht2?p=preview.因为Plunker是用Anguar构建的,所以很难用Batarang进行调试。我建议在本地下载代码。注释掉app.js的第10行,以使其使用共享作用域。
Why ng-transclude’s scope is not a child of its directive’s scope if the directive has an isolated scope?
ng-transclude旨在允许指令使用任意内容,隔离的范围旨在允许指令封装其数据。
如果ng-transclude没有保留这样的范围,那么任何被转录的任何内容都需要知道指令的实现细节(即它需要知道你创建的孤立范围上的可用内容)。
If it’s not a bug,how can a container directive pass data to it’s content,if not by setting attributes like I tried.
如果容器指令和包含的指令是耦合的 – 即你写了它们并且需要他们一起行动,那么它们应该通过共享控制器通信。
如果容器指令应该将内容注入到孩子的范围内(例如ng-repeat),那么你不应该使用一个孤立的范围。
角度文档很清楚的是什么行为应该是:
“In a typical setup the widget creates an isolate scope,but the transclusion is not a child,but a sibling of the isolate scope. This makes it possible for the widget to have private state,and the transclusion to be bound to the parent (pre-isolate) scope.”
Java Math.random()与Random.nextInt(int)
是什么区别Math.random() * n和Random.nextInt(n)在那里n是一个整数?
Matlab VS Python-eig(A,B)VS sc.linalg.eig(A,B)
我有以下矩阵sigma和sigmad:
sigma:
1.9958 0.7250
0.7250 1.3167
sigmad:
4.8889 1.1944
1.1944 4.2361
如果我尝试解决python中的广义特征值问题,则会获得:
d,V = sc.linalg.eig(matrix(sigmad),matrix(sigma))
V:
-1 -0.5614
-0.4352 1
如果我尝试在matlab中解决ge问题,我将获得:
[V,d]=eig(sigmad,sigma)
V:
-0.5897 -0.5278
-0.2564 0.9400
但是d确实是巧合。
关于Python:为什么`random.randint和a,b`返回一个包含`b`的范围?的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于*#【Python】【基础知识】【模块】【random】【使用random创造一个随机数】、angularjs – 为什么ng-transclude的范围不是它的指令范围的子 – 如果指令有一个孤立的范围?、Java Math.random()与Random.nextInt(int)、Matlab VS Python-eig(A,B)VS sc.linalg.eig(A,B)等相关内容,可以在本站寻找。
本文标签: