# Generate 0s and 1s

Hi, all,

Can anyone tell me which function in Python could I use to generate a
fixed length of 0s and 1s? I know in matlab we could just use
randsrc(x, y, 0:1), while I’ve no idea how to implement it in Python.
Thanks for ur help!

yufeng wang wrote:

Can anyone tell me which function in Python could I use to generate a
fixed length of 0s and 1s? I know in matlab we could just use
randsrc(x, y, 0:1), while I’ve no idea how to implement it in Python.
Thanks for ur help!

Look at the linear shift feedback register (gr_lfsr_32k_source_s) block.
It should do what you want.

@(^.^)@ Ed

yufeng wang wrote am 2009-02-13 14:34:

Hi, all,

Can anyone tell me which function in Python could I use to generate a
fixed length of 0s and 1s? I know in matlab we could just use
randsrc(x, y, 0:1), while I’ve no idea how to implement it in Python.
Thanks for ur help!

If you want to have random sources in GNU Radio, you can use various
sources, like a noise source or a LSFR source[2].

One method for a fixed length:

import random
length = 10
rand_sequence = random.sample(length/2*[1]+length/2*[0],length)

This first generates a list of 1s and 0s and permutates them. You can
vary the length/2-part to get a not-even-distribution.
Or:
rand_sequence = [x%2 for x in random.sample(xrange(10000000), length)]

These may not be the most efficient or random one, but it should do for
a first shot. For really long sequences yu should construct a sequence
from a random source with multiple invocations, like multiple
random.choice([0,1]) if you need reproducible numbers, or
random.SystemRandom([0,1]) to get good non-reproducible numbers. Read
all about random numbers and Python in [0]

If you are unfamiliar with Python, just read the Python Tutorial[1],
it’s really great.

Patrick

Engineers motto: cheap, good, fast: choose any two
Patrick S.
Student of Telematik, Techn. University Graz, Austria