Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Infinite random number generator

This example was created to show that iterators (generators) don't necessarily return a fixed list of values.

import random

def miranda():
    while True:
        yield random.random()

total = 0
for val in miranda():
    print(val)
    total += val
    if total > 10:
        break

Output: