Searching \ for '[PIC]: Random Number Sequence' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/math/index.htm?key=random
Search entire site for: 'Random Number Sequence'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: Random Number Sequence'
2006\06\11@172423 by Phil Keller

flavicon
face
All,

 I have an application that I need to generate a random number sequence
for.  Not just a random number, I know how to do that, but an entire
pseudo random sequence.  The sledge-hammer approach would be to generate
a random number, see if it is already in the sequence and store it if it
is new, then generate a new random number.  Early in the process this
would work OK, but when I need the last four numbers the time required
to generate the numbers could be excessive to say the least.

The details:
-    The user will have a random number of sound files (2 to 127) on a
memory card.
-    The PIC will count the number of available files and create a
random number sequence that is non-repeating and inclusive of all file
numbers.  (Each file is assigned a number so that I don't need to
'shuffle' the file names, just the numbers.)  I can skip files numbers
that are greater than the number available but a sequence that stops at
the maximum would be nice.
-    The PIC will select the first file number and send the file to a
player.
-    Once that file is played the PIC will select the next file number
in the sequence and send it to the player.
-    Once all songs have been played once, the PIC will generate a new
random sequence and start over.

 Suggestions or pointers are greatly appreciated.

Phil


2006\06\11@175940 by William Chops Westfield

face picon face

--Apple-Mail-10--42713177
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
       charset=US-ASCII;
       format=flowed


On Jun 11, 2006, at 2:22 PM, Phil Keller wrote:
>
> I have an application that I need to generate a random number
> sequence for.  Not just a random number, I know how to do that,
> but an entire pseudo random sequence.

You want a "shuffle" algorithm.  With a similar problem some time
ago, I came up with this framework (in C):

The enclosed code in shuffle_lib.c will return a shuffled sequence of
any
number of items up to 65535, using only 6 bytes or less of storage.  A
pseudo-random number generator (PRNG) can be used to produce a
non-repeating
sequence of numbers, but the usual implementation will generate only a
sequence of length 2^N-1 (for an N bit number.)  That's not a problem,
you
can just filter out all the numbers that are outside the range of
interest.
The other problem with a PRN is that the sequence itself repeats; 12
always
follows 232, or whatever.  Since our PRNG is generating a larger range
than
we want anyway, this can be fixed by permuting the number somehow before
filtering, using a separate permutation constant.  This means that our
final
algorithm is something like:

    shufflednum(max) = filter(max, permute(prng(randreg),
permuteconst));

The code example enclosed uses a standard shift-with-xor-feedback
scheme for
the PRNG, simple subtraction for the permutation, and a test against the
maximum as the filter, making it quite fast.  (however, it needs to be
fast
if the number of items to be shuffled is much smaller than the size of
the
PRNG.  to get 10 items with a 16 bit PRN, you may end up filtering out
65525
numbers.  This can be optimized by using a shorter PRN, of course.)  
Note
that by changing the PRNG seed, you change the starting position of the
shuffled list, but not the list itself, while changing the permutation
constant results in dramatically different sequences.

Enclosed files:

shuffle_lib.c: library functions in hopefully machine-independent C code
shuffle_test.c: demonstration program, written for unix, tested on
Solaris
shuffle_log.c: sample run, demonstrating effects of seed vs permutation
               constant changes



--Apple-Mail-10--42713177
Content-Transfer-Encoding: base64
Content-Type: application/zip; x-mac-type=5A495020; x-unix-mode=0755;
       x-mac-creator=53495421; name="Shuffle.zip"
Content-Disposition: attachment;
       filename=Shuffle.zip

UEsDBBQAAAAIACy2uTAnbZBSHwIAAPwFAAAPAAAAc2h1ZmZsZV9sb2cudHh0hZRNj5swEIbv/hWj
lXJjqW0wJJH20ktP/VDbW7WqWDABLbFTMO3uv69nEGZTleBIr8aeseN5ZvC3sm8vDgZX9E5XYA18
HzV8LF5BKhDZUSVHpUBynrLRtC9iB6eyhKEZ67rTP50eXLxMu/bJz+7tlZ/2yR3E765W2Y+hOF86
Df1oHhlr7J9zYV4jsHU9aBfBoHXFJAcBInjhwd8kAoDPFOSnYgpEi4EfAiVByXGWoSgUjiJRUhTy
HjBujxYKpCi0ljFcpGMkCp1AR3PG7lfG1vAZl01hTq05gWs0fPn66cN0eVrWA0xVQP/FDq1rrYng
aXRgrPOBv0ZtSh0DvLeuYT627ToorXFFa0D4pHOfr8/W5xrH8eNNohyRbiHFMKKxCuOK+BZxtop8
Ybo1tphedH8eXYHoEI3naRyc2t8ebgHnsWygauta99osRG82H7/dffyf9ssnXphfgJaEfiOHCgR4
6DdCRTtSorK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8yUW3VgdY3jIUvOr9EiNcglYmIqB48io6nOYEIyGxoMJaNkxnFHCUCcbOMp
VpWP+uqmR2Hv7IhcywKNFkFOFKQ9msLpHn464E+ZwGhdQd5fw+0SZmORci88yp8Lsc/tEzVRy0b/
Rq5kb5zBspZWB9Pnd9RzDjFkOH6m9QFjmuVUTJLz/GBCpJnvnSXWEEMNDXthwZ0d0l5SlVPK+WuK
1B85OP9lV6ZE7dZfvg02sOG5Xs4WoOF2gNHh7GwMjx3dWxI8HvweRwIJMaR2ckOuM364YJM1nMD8
BpZLmF8nzWeVjAbgk8hov7g4HM//sf637mwnlULAttbUunxOH56EMFLb/fNKjy3vLv4AUEsBAhQD
FAAAAAgAErA2MuPj7O92BAAA1ggAAAsAAAAAAAAAAAAAACSBTAIAAHNodWZmbGUudHh0UEsBAhQD
FAAAAAgALLa5MFGqRAc/BQAADA0AAA0AAAAAAAAAAAAAACSB6wYAAHNodWZmbGVfbGliLmNQSwEC
FAMUAAAACAAstrkwJ22QUh8CAAD8BQAADwAAAAAAAAAAAAAAJIEAAAAAc2h1ZmZsZV9sb2cudHh0
UEsBAhQDFAAAAAgALLa5MDKLVIKdAgAAbAUAAA4AAAAAAAAAAAAAACSBVQwAAHNodWZmbGVfdGVz
dC5jUEsFBgAAAAAEAAQA7QAAAB4PAAAAAA==

--Apple-Mail-10--42713177
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

2006\06\11@181342 by Martin K

flavicon
face
There are many interesting methods of generating random numbers. Chaotic
differential equations are sometimes used as could be the mersenne
twister algorithm or the linear congruential generator.
You probably don't require such a thing, but there are many novel
techniques involving hardware generation such as heat patterns,
radiation, human interaction intervals, etc.
--
MK

Phil Keller wrote:

{Quote hidden}

2006\06\11@191922 by Jinx

face picon face
part 1 1096 bytes content-type:text/plain; (decoded 7bit)

> when I need the last four numbers the time required to
> generate the numbers could be excessive to say the least

What would you call excessive ? AIUI you want to create a
sequence from 2 (min) to 127 (max) using numbers once and
once only. I've done similar s/w in the past, with a very much
longer sequence, and didn't really notice a problem. Besides,
as you say later -

> The PIC will select the first file number and send the file to a
> player. Once that file is played the PIC will select the next file
> number in the sequence and send it to the player

That time during the playing of the first selection is also available
to the PIC for completing the sequence ? You need only have
the first number to start playing

Martin K wrote -

> there are many novel techniques involving hardware generation
> such as heat patterns, radiation, human interaction intervals, etc.

I've used this noise source method often

http://home.clear.net.nz/pages/joecolquitt/white_noise.html

The attached shift register circuit works well too


part 2 4337 bytes content-type:image/gif; (decode)


part 3 35 bytes content-type:text/plain; charset="us-ascii"
(decoded 7bit)

2006\06\11@193307 by Zik Saleeba

face picon face
The Knuth shuffle algorithm is fast and easy. From Wikipedia:

"...the Knuth shuffle or Fisher-Yates shuffle[1], is a linear-time
algorithm (as opposed to the previous O(n log n) algorithm if using
efficient sorting such as mergesort or heapsort) which involves moving
through the pack from top to bottom, swapping each card in turn with
another card from a random position in the part of the pack that has
not yet been passed through (including itself). Providing that the
random numbers are unbiased, this will always generate a random
permutation.

"Notice that great care needs to be taken in implementing the Knuth
shuffle; even slight deviations from the correct algorithm will
produce biased shuffles. For example, working your way through the
pack swapping each card in turn with a random card from any part of
the pack is an algorithm with nn different possible execution paths,
yet there are only n! permutations. A counting argument based on the
pigeonhole principle will clearly show that this algorithm cannot
produce an unbiased shuffle, unlike the true Knuth shuffle, which has
n! execution paths which match up one-to-one with the possible
permutations."

http://en.wikipedia.org/wiki/Shuffle#Shuffling_algorithms

Cheers,
Zik

On 12/06/06, Phil Keller <spam_OUTPhilTakeThisOuTspampkeller.net> wrote:
{Quote hidden}

> -

2006\06\11@234717 by Phil Keller

flavicon
face
Thanks that looks like exactly what I wanted.  I'll play with it tomorrow.

 Thanks again,
Phil

William Chops Westfield wrote:

{Quote hidden}

--
--------------------------------
Long ago when men cursed and beat the
ground with sticks,  it was called witchcraft..

Today, it's called golf.


More... (looser matching)
- Last day of these posts
- In 2006 , 2007 only
- Today
- New search...