# Forum: GNU Radio generate 0s and 1s

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
on 2009-02-13 15:43
```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!```
on 2009-02-13 16:13
```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```
on 2009-02-13 17:54
```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

[0] http://docs.python.org/library/random.html
[1] http://docs.python.org/tutorial/