Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6962985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:43:50+00:00 2026-05-27T15:43:50+00:00

I am trying to generate random text using letter frequencies that I have obtained.

  • 0

I am trying to generate random text using letter frequencies that I have obtained. First, I succeeded with the following code:

for i in range(450):
    outcome=random.random()
    if 0<outcome<0.06775:
        sys.stdout.write('a')
    if 0.06775<outcome<0.07920:
        sys.stdout.write('b')
    if 0.07920<outcome<0.098:
        sys.stdout.write('c')
    ....

This until the letter z and spacebar. This give me >50 lines of code and I want to get the same result using an array.

So far I have :

f_list = [0, 0.06775, 0.08242, 0.10199, 0.13522, 0.23703, 0.25514, 0.27324, 0.32793, 0.38483, 0.38577, 0.39278, 0.42999, 0.45023, 0.50728, 0.56756, 0.58256, 0.58391, 0.62924, 0.68509, 0.7616, 0.78481, 0.79229, 0.81161, 0.81251, 0.82718, 0.82773, 0.99998]
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ' ']

import random
import sys

for i in range(25):
    outcome=random.random()
    if f_list[i]<outcome<f_list[i+1]:
        sys.stdout.write('alphabet[i]')

But it isn’t working properly, as the range seems now to relate to the array and not the number of iterations I want. The output is blank.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:43:50+00:00Added an answer on May 27, 2026 at 3:43 pm
    import random
    import sys
    import bisect
    
    f_list = [0, 0.06775, 0.08242, 0.10199, 0.13522, 0.23703, 0.25514, 0.27324, 0.32793, 0.38483, 0.38577, 0.39278, 0.42999, 0.45023, 0.50728, 0.56756, 0.58256, 0.58391, 0.62924, 0.68509, 0.7616, 0.78481, 0.79229, 0.81161, 0.81251, 0.82718, 0.82773, 0.99998]
    alphabet = 'abcdefghijklmnopqrstuvwxyz '
    
    for i in xrange(450):
        sys.stdout.write(alphabet[bisect.bisect(f_list, random.random()) - 1])
    

    does the trick and returns (example):

    l wefboethol gsplotfoh ua onpedefh dnolnairnioeiegehhecaworonnfmeuej dsiauhpbfttwcknal ateof ap cgbr sunnee leseaeeecltaiaur u oen vxntgsoio kdeniei ot df htr dcencrsrrfp bwelsuoaslrnr heh ee tpt oeejaldeatcto fi a u idimiadmgglral o m iaielbtnt es oe shlspudwdfrrsvol oo i tlwh d r i swhsnloai p swlooi wbe nn sshth nsawtnrqsud mtw diit pner r nitmah todf zcsehma hl e ros ctee toiouinn i hl hlonphioe nh gan ho heein itrgeylftn epaacrmanhe

    alphabet can be defined as a simple string too (accessing its elements – single characters – works like for lists)

    bisect.bisect(list, value) takes a sorted list and a value and tells where this value should be put between. More about bisect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using this code to generate random text : from collections import defaultdict,
I'm trying to wrtie a facebook application that generates random quotes using php. However,
I'm trying to generate a random number that's between 0 and 1. I keep
I have a website. On this website, I am trying to generate random images.
I'm trying to generate a random int that is either 0 or 1 in
I am trying to generate a random number that only populates a certain percent
I am trying to generate random Unicode characters with two starting number+letter combination.. I
I'm trying to generate a random real number between 0 and 1, using the
I'm trying to generate a random string using this command: strings /dev/urandom | grep
I have some code to generate 4 unique random numbers between 0-9: - //Globals

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.