Searching \ for 'PIC Pseudo Random Code' 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: 'PIC Pseudo Random Code'.

Truncated match.
PICList Thread
'PIC Pseudo Random Code'
1998\02\17@080521 by David Duffy

flavicon
face
Does anyone have any code suitable for generating pseudo random
numbers in a presettable range ?  (say 1-20 or 1-50) Or perhaps
some way of randomly re-ordering the numbers 1-20 in a table ?
What I want to do is generate a table of (say 20) numbers in a
sequence such as 2,15,8,1,19,12,etc with all numbers up to the
specified max used. It's a bit like Bingo where eventually every
number will come up but not in any obvious order. Also, each
"run" sequence needs to be different from the last one. I know
that it's a tall order but even a starting point would help.
Regards...
_______________________________________________________________
Dave Duffy         Audio Visual Devices           spam_OUTAVDTakeThisOuTspamuq.net.au
Unit 8, 9-11 Trade Street, Cleveland, Queensland 4163 Australia
Phone: +61 7 38210362                 Facsimile: +61 7 38210281
_______________________________________________________________

1998\02\17@123240 by Craig Webb

flavicon
face
Dear Dave:

At 10:26 PM 17-2-98 +1100, you wrote:
>Does anyone have any code suitable for generating pseudo random
>numbers in a presettable range ?  (say 1-20 or 1-50) Or perhaps
>some way of randomly re-ordering the numbers 1-20 in a table ?
>What I want to do is generate a table of (say 20) numbers in a
>sequence such as 2,15,8,1,19,12,etc with all numbers up to the
>specified max used. It's a bit like Bingo where eventually every
>number will come up but not in any obvious order. Also, each
>"run" sequence needs to be different from the last one. I know
>that it's a tall order but even a starting point would help.

The most "random" method I would suggest, to have different run sequences,
is to have a counter incremementing in your main code loop and then to copy
it to a  random "seed" variable with each button press (i.e. maybe the
counter is cycling during your "Button-press" wait loop). Then, once you get
it, you can XOR it with the RTCC or something if you really want something
random.

Hope this helps.

C. Webb

1998\02\18@050220 by David Duffy

flavicon
face
>>Does anyone have any code suitable for generating pseudo random
>>numbers in a presettable range ?  (say 1-20 or 1-50) Or perhaps
>>some way of randomly re-ordering the numbers 1-20 in a table ?
>>What I want to do is generate a table of (say 20) numbers in a
>>sequence such as 2,15,8,1,19,12,etc with all numbers up to the
>>specified max used. It's a bit like Bingo where eventually every
>>number will come up but not in any obvious order. Also, each
>>"run" sequence needs to be different from the last one. I know
>>that it's a tall order but even a starting point would help.

>Craig Webb <.....lucidKILLspamspam@spam@MAGNET.CA> wrote;
>The most "random" method I would suggest, to have different run sequences,
>is to have a counter incremementing in your main code loop and then to copy
>it to a  random "seed" variable with each button press (i.e. maybe the
>counter is cycling during your "Button-press" wait loop). Then, once you get
>it, you can XOR it with the RTCC or something if you really want something
>random.

Sounds good but there's one small glitch - no push button !  The code runs
all by itself with no user input. I can see how I could let the RTCC run and
pick a seed for random routine that way, as the unit gets feedback from other
equipment every hour or so and that's when I need the new random run
sequence. The time period between events varies between 50 and 70 minutes.
But I can't
see a way to generate the list of all 20 numbers in random order. The only
thing that springs to mind is generating lots of random numbers and throwing
away any that are out of desired range or are duplicates. Maybe I'm missing
an obvious solution but it has been a long day !
Regards...
_______________________________________________________________
Dave Duffy         Audio Visual Devices           AVDspamKILLspamuq.net.au
Unit 8, 9-11 Trade Street, Cleveland, Queensland 4163 Australia
Phone: +61 7 38210362                 Facsimile: +61 7 38210281
_______________________________________________________________

1998\02\18@072425 by Clyde Smith-Stubbs

flavicon
face
On Wed, Feb 18, 1998 at 07:42:19PM +1100, David Duffy wrote:

> But I can't
> see a way to generate the list of all 20 numbers in random order. The only

If you can generate one truly random seed, you can then use a pseudo-rando
number generator to generate a sequence that will be, for your purposes,
random. But if your seed is not truly random, you'll get the same sequences
repeating.

Here's a pseudo-random number generator (from the HI-TECH PIC C library)
.

You call srand with a random seed, then successive calls to rand() will return
a pseudo-random sequence based on that seed. The function is chaotic, i.e. a
small
variation in the seed causes large variations in the sequence, but it
is deterministic, so it will only be as random as your seed.



#include        <stdlib.h>

static  long    randx;
static  char    randf;

void
srand(unsigned x)
{
       randx = x;
       randf = 1;
}

int
rand(void)
{
       if(!randf)
               srand(1);
       return((int)((randx = randx*1103515245L + 12345)>>16) & 077777);
}
--
Clyde Smith-Stubbs               |            HI-TECH Software
Email: .....clydeKILLspamspam.....htsoft.com          |          Phone            Fax
WWW:   http://www.htsoft.com/    | USA: (408) 490 2885  (408) 490 2885
PGP:   finger EraseMEclydespam_OUTspamTakeThisOuThtsoft.com   | AUS: +61 7 3354 2411 +61 7 3354 2422
---------------------------------------------------------------------------
ANSI C for the PIC! Now shipping! See http://www.htsoft.com for more info.

1998\02\18@134019 by Mike Keitz

picon face
On Wed, 18 Feb 1998 19:42:19 +1100 David Duffy <AVDspamspam_OUTUQ.NET.AU> writes:
>But I can't
>see a way to generate the list of all 20 numbers in random order.

If the number of list elements is rather small (<100) try this (for 20):

Define 20 one-bit flags.  Start with them all clear.

Each time you want the next element in the list, count how many flags are
clear.  If it's 0, the whole list is done.  If it's 1, then only that
number remains.  Otherwise, generate a random number from 1 to the number
of clear flags.  One way to generate random numbers over an arbitrary
range is to divide a large random number by the range and use the
remainder (this isn't quite uniformly random, but if the "large" number
is large enough, it's very close).

Issue the number that corresponds to that flag location (search up
through the flags for the n'th clear one) and set the flag so that number
won't be issued again.

This process has a very definite, once-through method to it, so it
shouldn't get stuck (for a random time) like one based on searching and
retrying could.


_____________________________________________________________________
You don't need to buy Internet access to use free Internet e-mail.
Get completely free e-mail from Juno at http://www.juno.com
Or call Juno at (800) 654-JUNO [654-5866]

1998\02\18@162320 by Tom Mariner

flavicon
face
Hello Dave,

If this is for a commercial game, here is a lesson I learned from doing a
(Pic-based) game for one of the major US toy firms -- Just about any simple
pseudo random sequence no matter how started with true random seed can be
guessed by kids! An answer to your question is to save the start seed then
remember the incremental offset to get to the n'th "random" number. Our
solution was to "play" one random method against another to get to a
difficult to match sequence.

Tom

On Tuesday, February 17, 1998 6:27 AM, David Duffy [SMTP:@spam@AVDKILLspamspamUQ.NET.AU]
wrote:
{Quote hidden}

1998\02\18@225452 by Rick Dickinson

flavicon
face
At 07:42 PM 2/18/98 +1100, David Duffy wrote:
>But I can't
>see a way to generate the list of all 20 numbers in random order. The only
>thing that springs to mind is generating lots of random numbers and throwing
>away any that are out of desired range or are duplicates. Maybe I'm missing
>an obvious solution but it has been a long day !

The way that I have always generated lists of random, non-repeating numbers
(like shuffling a deck of cards) is to start off with an array of all of
the numbers in order.  I generate a bunch of random numbers in the same
range, and use those random numbers in pairs as the array indices of
numbers to swap.

For instance, start off with the array a=[1 2 3 4 5 6]

Assuming indices run from 1 to 6, a(1)=1, a(2)=2, ... a(6)=6

Pick two random numbers, let's say 3 & 5.... swap a(3) with a(5).

Now, the array is [1 2 5 4 3 6].

Pick two random numbers, let's say 2 & 5.... swap a(2) with a(5).

Now, the array is [1 3 5 4 2 6].

Keep picking pairs of positions to swap for at least 2n times, where n is
the number of positions.

To scale random numbers to a desired range, just divide by the size of the
range and take the remainder.  If your range is an even power of two, just
mask off the appropriate number of bits from your random number.

Hope this helps,

- Rick "Markoff Chain" Dickinson

+---------------------------------+---------------------------+
| Enterprise ArchiTechs Company   |"You can't reason someone  |
|     Lotus Certified Notes       |  out of a position they   |
|  Appl. Design & Administration  | didn't reason themselves  |
|(818)563-1061  RemoveMErtdTakeThisOuTspamnotesguy.com  |  into" -- Rick Adams,     |
|   http://www.eArchiTechs.com    |   in alt.folklore.urban   |
+---------------------------------+---------------------------+

1998\02\19@041111 by Ivan Kocher

flavicon
face
Maybe you could use an external oscillator, like a LM555.  This device
doesn't have a very stable oscillating frequency, you might use that as
a base for randomizing your internal (software) generator.
That also goes for temp sensing, supply (110VAC) line, or what ever is
outside your PIC.

Is the device near someone passing by?  You might detect something near
with a high frequency oscillator, which varies it's frequency as
someone(thing) gets near.  Another way of generator a base for the
generator.  One totally random, it depends on the _outside_ of your PIC.

Ivan Kocher

David Duffy wrote:
{Quote hidden}

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