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的更多相关文章

  1. Chrome V8引擎系列随笔 (1):Math.Random()函数概览

    先让大家来看一幅图,这幅图是V8引擎4.7版本和4.9版本Math.Random()函数的值的分布图,我可以这么理解 .从下图中,也许你会认为这是个二维码?其实这幅图告诉我们一个道理,第二张图的点的分 ...

  2. Math.random()

    Math.random() 日期时间函数(需要用变量调用):var b = new Date(); //获取当前时间b.getTime() //获取时间戳b.getFullYear() //获取年份b ...

  3. .Net使用system.Security.Cryptography.RNGCryptoServiceProvider类与System.Random类生成随机数

    .Net中我们通常使用Random类生成随机数,在一些场景下,我却发现Random生成的随机数并不可靠,在下面的例子中我们通过循环随机生成10个随机数: ; i < ; i++) { Rando ...

  4. 随机数(random)

    需求 Random rd=new Random(); 需要十以内的随机数  (0---10) System.out.println((int)((rd.nextDouble()*100)/10)); ...

  5. python写红包的原理流程包含random,lambda其中的使用和见简单介绍

    Python写红包的原理流程 首先来说说要用到的知识点,第一个要说的是扩展包random,random模块一般用来生成一个随机数 今天要用到ramdom中unifrom的方法用于生成一个指定范围的随机 ...

  6. [LeetCode] Random Pick Index 随机拾取序列

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...

  7. [LeetCode] Linked List Random Node 链表随机节点

    Given a singly linked list, return a random node's value from the linked list. Each node must have t ...

  8. [LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表

    A linked list is given such that each node contains an additional random pointer which could point t ...

  9. php对应js math.random

    <?php function random($min = 0, $max = 1) {     return $min + mt_rand()/mt_getrandmax()*($max-$mi ...

  10. python常用模块(模块和包的解释,time模块,sys模块,random模块,os模块,json和pickle序列化模块)

    1.1模块 什么是模块: 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文 ...

随机推荐

  1. 1067. Sort with Swap(0,*) (25)【贪心】——PAT (Advanced Level) Practise

    题目信息 1067. Sort with Swap(0,*) (25) 时间限制150 ms 内存限制65536 kB 代码长度限制16000 B Given any permutation of t ...

  2. 集群版本升级——rolling upgrade在ES 单节点从 restart 到加入集群,大概要 100s 左右的时间。也就是说,这 100s 内,该节点上的所有分片都是 unassigned 状态

    集群版本升级 Elasticsearch 作为一个新兴项目,版本更新非常快.而且每次版本更新都或多或少带有一些重要的性能优化.稳定性提升等特性.可以说,ES 集群的版本升级,是目前 ES 运维必然要做 ...

  3. Linux就该这么学 20181004(第六章磁盘管理)

    参考链接https://www.linuxprobe.com/ /boot 开机锁需要文件-内核.开机菜单以及所需配置文件 /dev 以文件形式存放的任何设备与接口 /etc 配置文件 /home 用 ...

  4. Kali linux 2016.2(Rolling)的利用MSF攻击windows小案例(exploits + payloads + taegets)(博主推荐)

    不多说,直接上干货! 本博文举的例,是利用远程溢出windows服务器. Exploit/windows/smb/ms08_067_netapi. Payload :    windows/shell ...

  5. VisualStudio UnitTest FrameWork

    当创建单元测试时,Microsoft.VisualStudio.TestTools.UnitTesting的名字控件会添加到测试项目中,该名字控件中包含很多有用的类: 断言类:在单元测试中验证条件 初 ...

  6. MySQL学习(一)——启动和登录MySql遇到的问题及解决

    1.MySQL使用命令行启动时报错“发生系统错误 5”,如下: 原因:未用管理员身份运行cmd程序 解决方案:在开始菜单里找到命令提示符,右击选择以管理员身份运行 2.登录时报错“提示mysql不是内 ...

  7. Base64就是一种 基于64个可打印字符来表示二进制数据的表示方法

    Base64编码是从二进制到字符的过程. Base64编码主要用在传输.存储.表示二进制等领域,还可以用来加密,但是这种加密比较简单. byte[] byteArray = Encoding.UTF8 ...

  8. 【原创】JMS生产者和消费者【PTP异步接收消息】

    PTP模式下,异步接收消息需要定义一个MessageListener来监听,当生产者有消息要发送时会主动通知Listener去处理该消息,会调用监听的onMessage方法去处理. 首先看生产者(和同 ...

  9. Swift 中的协议

    Swift 中的协议协议是为方法.属性等定义一套规范,没有具体的实现,类似于Java中的抽象接口,它只是描述了方法或属性的骨架,而不是实现.方法和属性实现还需要通过定义类,函数和枚举完成. 协议定义 ...

  10. pthread_cleanup_push

    #define pthread_cleanup_push(func, val) \ { \ struct __darwin_pthread_handler_rec __handler; \ pthre ...