python中生成随机整数_在Python中生成0到9之间的随机整数

python中生成随机整数

Following are the few explanatory illustrations using different python modules, on how to generate random integers? Consider the scenario of generating the random numbers between 0 and 9 (both inclusive).

以下是使用不同的python模块的一些说明性插图,说明如何生成随机整数? 考虑生成介于0和9之间(包括两端)的随机数的方案。

使用randrange (Using randrange )

Syntax:

句法:

    random.randrange(stop)
random.randrange(start, stop, step)

Code:

码:

>>> import random
>>> for i in range(10):
...     print(random.randrange(10))
... 
2
2
2
0
8
8
5
6
6
3

使用randint (Using randint)

Syntax:

句法:

    random.randint(a,b)

Code:

码:

>>> import random
>>> for i in range(10):
...     print(random.randint(0,10))
... 
1
6
7
5
8
9
6
2
3
9
>>>

使用机密 (Using secrets)

By using this method, we can generate cryptographically strong random numbers.

通过使用此方法,我们可以生成加密强度高的随机数。

>>> from secrets import randbelow
>>> for i in range(10):
...     print(randbelow(10))
... 
6
5
2
0
7
2
0
1
2
6
>>> 

翻译自: https://www.includehelp.com/python/generate-random-integers-between-0-and-9-in-python.aspx

python中生成随机整数

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.pswp.cn/news/379707.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

愚人节恶搞网站谨防遭黑客攻击

金山毒霸云安全中心日前发出预警,在近期拦截的大量“挂马”、钓鱼等恶意网页中,与“愚人节”相关的,在近一周数量急剧增加。 愚人节将至,怎么整人好玩?近期许多恶搞网站、相关的网络论坛的流量不断攀升。金山毒霸云安全中心日前发…

JavaScript中的String()函数与示例

String()函数 (String() function) String() function is a predefined global function in JavaScript, it is used to convert an object to the string. String()函数是JavaScript中预定义的全局函数,用于将对象转换为字符串。 Example: 例: In thi…

ASCII码排序(C++)

题目描述: 输入三个字符(可以重复)后,按各字符的ASCII码从小到大的顺序输出这三个字符。 输入描述: 第一行输入一个数N,表示有N组测试数据。后面的N行输入多组数据,每组输入数据都是占一行,有三个字符组成,…

FreeRTOS--堆内存管理(二)

堆内存管理代码具体实现heap_1内存申请函数内存释放函数heap_2内存块内存堆初始化函数内存块插入函数内存申请函数判断是不是第一次申请内存开始分配内存内存释放函数heap_3heap_4内存堆初始化函数内存块插入函数heap_5上一篇文章说了FreeRTOS实现堆内存的原理,这一…

在查询的结果中添加自增列 两种方法

解决办法《一》: 在SQL Server数据库中表信息会用到Identity关键字来设置自增列。但是当有数据被删除的话,自增列就不连续了。如果想查询出这个表的信息,并添 加一列连续自增的ID,可用如下查询语句: SELECT Row_Nu…

一个非常简单的C#面试题

怎样实现对所有类可读但是在同一个assembly可写那? 答案: 同一个assembly namespace ClassLibrary1 { public class Class1 { public string Name { get; internal set; } } public class Class2 { public void GS() { Class1 cc new Class1(); cc.Name…

css中的node.js_在Node App中使用基本HTML,CSS和JavaScript

css中的node.jsYou may think this is not important, but it is!. As a beginner in node.js, most coding exercises are always server sided. 您可能认为这并不重要,但确实如此! 作为node.js的初学者,大多数编码练习始终都是服务器端的。…

Binary String Matching(C++)

题目描述: Given two strings A and B, whose alphabet consist only ‘0’ and ‘1’. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is ‘1001110110’ while the pattern string A is ‘11’, you should…

由一次代码优化想到的Js 数据类型

引子: 上周三进行了代码优化,其中有一个很普遍的代码,例如: if(test "") {dothis();}else{dothat()} ----->可以简化为 !test ? dothis():dothat(); if(test "") {dothis()}     ----->可以简化为…

VisualStudio2019配置OpenCV

VisualStudio2019配置OpenCV配置0x01 准备0x02 配置系统环境0x03 复制文件0x04 配置VisualStudio2019测试配置 0x01 准备 下载opencv,官网地址:https://opencv.org/releases/# 下载之后,自行安装 0x02 配置系统环境 找到高级系统设置 …

转载 Javascript的IE和Firefox兼容性汇编

微软关于IE、Firefox、Opera和Safari的JavaScript兼容性研究曾经发表过一份草案,可以点击下载《JScript Deviations from ES3》 以下为网上的一些搜集和整理(FF代表Firefox) 集合类对象问题现有代码中存在许多 document.form.item("itemName") 这样的语句&#xff0c…

存储器间接寻址方式_8086微处理器的程序存储器寻址模式

存储器间接寻址方式The Program Memory Addressing mode is used in branch instructions. These branch instructions are instructions which are responsible for changing the regular flow of the instruction execution and shifting the control to some other location…

Servlet的配置

1&#xff0c;基本配置 <!-- Servlet类的配置 --><servlet><servlet-name>sq</servlet-name><servlet-class>beyond.servlet.QuickStartServlet</servlet-class></servlet><!-- Servlet的虚拟路径的配置 --> <servlet-mapp…

Asp.net页面生存周期

# 事件或方法 功能 描述   1 Init 事件 页面初始化 初始化设置。   2 LoadViewState 方法 加载视图状态 填充ViewState属性。   3 LoadPostData 方法 处理回发数据 处理传入窗体数据。   4 Load 事件 加载页面 页面控件初始化完成并反映了客户端的数据。   5 RaisePo…

你正确关闭WCF链接了吗?

通常情况下我们关闭一个WCF链接都是简单地写把ICommunicationObject.Close()方法&#xff0c;但是这个方法有个问题就是当调用发生异常时&#xff0c;Close()会发生次生的异常&#xff0c;导致链接不能正常关闭。如果当这种异常很多时&#xff0c;必然对系统的稳定性有很大的影…

Visual Studio进行linux远程开发

目录准备工作创建一个项目配置远程项目准备工作 查看linux IP地址 安装了工具 sudo apt-get install openssh-server g gdb make ninja-build rsync zip开启ssh服务&#xff1a; sudo service ssh startVS2019按装了linux功能&#xff0c;如果没有&#xff0c;找到Visual S…

在给定总和K的二叉树中找到级别

Description: 描述&#xff1a; The article describes how to find the level in a binary tree with given sum K? This is an interview coding problem came in Samsung, Microsoft. 本文介绍了如何在给定总和K下在二叉树中找到级别 &#xff1f; 这是一个面试编码问题&a…

PostgreSQL学习手册(数据库维护) 转

原文&#xff1a; PostgreSQL学习手册(数据库维护)一、恢复磁盘空间&#xff1a;在PostgreSQL中&#xff0c;使用delete和update语句删除或更新的数据行并没有被实际删除&#xff0c;而只是在旧版本数据行的物理地址上将该行的状态置为已删除或已过期。因此当数据表中的数据变化…

++i与i++的根本性区别(两个代码对比搞定)

首先来看i 代码如下&#xff1a; #include <stdio.h> #include <stdlib.h> int main() {int i0;int ai;printf("%d\n",a);printf("%d\n\n\n",i);return 0; }输出结果如下&#xff1a; 解释&#xff1a;i其实是两行代码的简写形式&#xff0c…

国企和外企的比较

由于本人在外企&#xff0c;而很多朋友在国企&#xff0c;因此我个人的说法应该还是有一定的权威性。 首先&#xff0c;国企和外企不能一概而论。正如任何事物都有三六九等&#xff0c;这个&#xff0c;只能在同等级别上进行比较。 国企分类&#xff1a; 一等国企&#xff1…