python生成随机数、随机字符串

import random
import string

# 随机整数:
print random.randint(1,50)

# 随机选取0到100间的偶数:
print random.randrange(0, 101, 2)

# 随机浮点数:
print random.random()
print random.uniform(1, 10)

# 随机字符:
print random.choice('abcdefghijklmnopqrstuvwxyz!@#$%^&*()')

# 多个字符中生成指定数量的随机字符:
print random.sample('zyxwvutsrqponmlkjihgfedcba',5)

# 从a-zA-Z0-9生成指定数量的随机字符:
ran_str = ''.join(random.sample(string.ascii_letters + string.digits, 8))
print ran_str

# 多个字符中选取指定数量的字符组成新字符串:
prin ''.join(random.sample(['z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a'], 5))

# 随机选取字符串:
print random.choice(['剪刀', '石头', '布'])

# 打乱排序
items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
print random.shuffle(items)

python生成随机数、随机字符串的更多相关文章

  1. 使用boost库生成 随机数 随机字符串

    #include <iostream> #include <boost/random/random_device.hpp> #include "boost/rando ...

  2. JMeter - 生成随机数/随机字符串/随机变量/随机日期

    1. Random - 随机数 1.1 作用 1.2 声明 1.3 例子 2. __RandomDate - 随机日期 2.1 作用 2.2 声明参数 2.3 例子 3. RandomString - ...

  3. 【python】【转】Python生成随机数的方法

    如果你对在Python生成随机数与random模块中最常用的几个函数的关系与不懂之处,下面的文章就是对Python生成随机数与random模块中最常用的几个函数的关系,希望你会有所收获,以下就是这篇文 ...

  4. Python生成随机数的方法

    这篇文章主要介绍了Python生成随机数的方法,有需要的朋友可以参考一下 如果你对在Python生成随机数与random模块中最常用的几个函数的关系与不懂之处,下面的文章就是对Python生成随机数与 ...

  5. C#使用 RNGCryptoServiceProvider 生成强随机字符串

    为了生成更加可靠的随机数,微软在System.Security.Cryptography命名空间下提供一个名为system.Security.Cryptography.RNGCryptoService ...

  6. python 生成随机数、生成 uuid

    1. 使用 uuid.uuid1 产生一个随机数 2. 在使用 random.sample() 产生一个随机字符串 3. 将两者进行拼接 import uuid import random def r ...

  7. Python生成随机数的一些函数

    头文件: import random 1.生成一个随机浮点数,范围是0-1: print random.random() 2.生成指定范围内的随机浮点数: print random.uniform(a ...

  8. [ Python入门教程 ] Python生成随机数模块(random)使用方法

    1.使用randint(a,b)生成指定范围内的随机整数.randint(a,b)表示从序列range([a,b])中获取一个随机数,包括b. >>> random.randint( ...

  9. Python 生成随机数

    import random x = int(input('Enter a number for x: '))  --随机数最小值y = int(input('Enter a number for y: ...

随机推荐

  1. 预期结果 参数化parametrize

    1.pytest.mark.parametrize装饰器可以实现测试用例参数化. 2.实例: import pytest @pytest.mark.parametrize("req,expe ...

  2. Vue项目初始化

    1. 生成项目模板 vue init <模板名> 本地文件夹名称2. 进入到生成目录里面 cd xxx npm install3. npm run dev vue项目模板介绍: simpl ...

  3. HTC脚本介绍和入门示例

    一.简介 HTC脚本全称是Html Companent即html组件,个人认为它是为了在开发动态HTML中实现代码重用和页面共享目的,主要是把“行为”作为组件封装,可以在很大程度上简化DHTML的开发 ...

  4. ubantu创建python虚拟环境

    安装虚拟环境的命令如下: sudo pip install virtualenv sudo pip install virtualenvwrapper 创建虚拟环境的命令如下: mkvirtualen ...

  5. ChainingHash

    public class ChainingHash<Key,Value>{ private int N; private int M; private doublylinked<Ke ...

  6. Semaphore计数信号量

    ExecutorService exec = Executors.newCachedThreadPool(); final Semaphore semp = new Semaphore(5); for ...

  7. 终极C语言指针

    // ex1.cpp : Defines the entry point for the console application. // #include "stdafx.h" # ...

  8. 2.11 alert\confirm\prompt

    2.11 alert\confirm\prompt 前言   不是所有的弹出框都叫alert,在使用alert方法前,先要识别出到底是不是alert.先认清楚alert长什么样子,下次碰到了,就可以用 ...

  9. url的反向解析

    1. url的语法格式: url(regex, views, **kwargs, name) name:为地址起别名,反向解析时使用 2.反向解析 对于Django中的url反向解析,是分模板和视图的 ...

  10. [LeetCode&Python] Problem 762. Prime Number of Set Bits in Binary Representation

    Given two integers L and R, find the count of numbers in the range [L, R](inclusive) having a prime ...