[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
## [Discuss-gnuradio] Re: generate 0s and 1s

**From**: |
Patrick Strasser |

**Subject**: |
[Discuss-gnuradio] Re: generate 0s and 1s |

**Date**: |
Fri, 13 Feb 2009 16:53:18 +0100 |

**User-agent**: |
Thunderbird 2.0.0.19 (Windows/20081209) |

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/
[2] http://www.gnuradio.org/doc/doxygen/group__source.html
--
Engineers motto: cheap, good, fast: choose any two
Patrick Strasser <patrick dot strasser at student dot tugraz dot at>
Student of Telematik, Techn. University Graz, Austria