Random words

To choose a random word from the histogram, the simplest algorithm is to build a list with multiple copies of each word, according to the observed frequency, and then choose from the list:

The expression[word] * freq creates a list with freq copies of the string word (actually, to be more precise, the elements are references to the same string). The extend method is similar to append expect that the argument is a sequence.

This algorithm works, but it is wildly inefficient; each time you choose a random word, it rebuilds the list, which is as big as the original book.

If you generate a series of words from the book, you can get a sense of the vocabulary, but it probably won’t make much sense. The next section is about generating random text that makes more sense.

from Thinking in Python

Random words,布布扣,bubuko.com

时间: 2024-12-28 15:30:20

Random words的相关文章

numpy的random模块

[说明] 翻译自官网的文档. 随机抽样 (numpy.random) 简单的随机数据 rand(d0, d1, ..., dn) 随机值 >>> np.random.rand(3,2) array([[ 0.14022471, 0.96360618], #random [ 0.37601032, 0.25528411], #random [ 0.49313049, 0.94909878]]) #random randn(d0, d1, ..., dn) 返回一个样本,具有标准正态分布.

[TypeScript] Create random integers in a given range

Learn how to create random integers using JavaScript / TypeScript. /** * Returns a random int between * @param start inclusive * @param before exclusive */ export function randomInt(start: number, before: number) { return start + Math.floor(Math.rand

java中Random随机种子使用

在java中,通过Random生成随机数时,如果设置随机种子,则相同的种子,产生的随机数相同.若不设置则每次随机的不同. Random rnd = new Random(); rnd.setSeed(10);//用于设置种子. rnd.nextInt();// 用于产生随机数. rnd.nextInt(10); // 产生(0-9)数字.

random模块

首先random函数并不是一个真正的随机数,而是根据随机数算法算出来的数 random 函数返回一个从 0.0 到 1.0 的随机浮点数 (包括 0.0, 但是不包括 1.0).每次,调用 random,就会的到一个随机数. >>> import random >>> random.random()  0.9536935859948081  >>> random.random()  0.40421571099356646  randint 函数接受一个

python标准库-random学习

参考资料:Python 2.7.7 documentation 参考工具:http://translate.google.cn/ random模块学习 一.Bookkeeping functions(几乎没看懂)     random.seed([x]) Initialize the basic random number generator     random.getstate() Return an object capturing the current internal state o

Python模块学习笔记— —random

Python中的random模块用于生成随机数. random.random 函数原型 random.random() 生成一个范围在[0,1)的随机浮点数. import random print random.random() random.uniform 函数原型 random.uniform(a,b) 生成一个指定范围内的随机浮点数,两个参数一个是上限,一个是下限.如果a > b,则生成的随机数范围在[b,a].否则, 范围在[a,b]. import random print rand

hdu 4790 Just Random 容斥原理+数学

Just Random Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1723    Accepted Submission(s): 483 Problem Description Coach Pang and Uncle Yang both love numbers. Every morning they play a game w

Python -- random 随机数生成

Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0 random.uniform random.uniform的函数原型为:random.uniform(a, b),用于生成一个指定范围内的随机符点数,两个参数其中一个是上限,一个是下限.如果a > b,则生成的随机数n: b <= n <= a.如果 a <

Java Random

http://blog.csdn.net/huangbiao86/article/details/6433964 java中Math.random()与java.util.random()的区别 public class Random1 { public static void main(String[] args) { // TODO 自动生成的方法存根 for(int i=10;i<30;i++){ System.out.println((int)(1+Math.random()*10));

Random Pick Index

Given an array of integers with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array. Note: The array size can be very large. Solution that uses too much extra sp