"random" generator algorithm for pic - Page 1
Forum section: electronics / projects

author: Simon
author_rank: Unknown
timestamp: December 30, 2009, 03:40:29 pm
content: ok, this is not as sientific as it may sound.We are going to have a birthday gathering for my sisters birthday and want to do a pass the parcel. I thought, so that we can all take part (well and to show off to the young ladies) I'll knock up a timer circuit but this needs to have random intervalls to make it more or less fair. So i suppose a pic comes in handy, so I could just preprogram some delays or make it self generate a random figure.What sort of mathematical formula do i use to generate a random number (and maybe have some control over min and max figure) ?of course this could make a good little project for many things

author: jahonen
author_rank: Super Contributor
timestamp: December 30, 2009, 03:56:40 pm
content: Maybe Maximum Length Sequence could be used? It is pseudorandom, thus it repeats itself eventually, but the period can be made arbitrarily long. Generation is very simple, which is a bonus for PIC implementation.Regards,Janne

author: Simon
author_rank: Unknown
timestamp: December 30, 2009, 04:21:39 pm
content: yes that sounds good, where do I get this algorithm from ?EDIT: Just looked it up on wikipedia, looks rather complex, and there is no practical explanation of implementing it (or it's because I'm rubbish at maths)

author: jahonen
author_rank: Super Contributor
timestamp: December 30, 2009, 05:42:13 pm
content: Math is great fogscreen for making simple things look much more complexNumerical Recipes in C gives easy C implementation (this example for 2^18-1 bits long random sequence):Code:#define IB1 1#define IB2 2#define IB5 16#define IB18 131072#define MASK (IB1+IB2+IB5)int irbit2(unsigned long *iseed){if (*iseed & IB18){*iseed = ((*iseed ^ MASK) << 1) | IB1;return 1;}else{*iseed <<= 1;}}Regards,Janne

author: Simon
author_rank: Unknown
timestamp: December 30, 2009, 06:00:33 pm
content: Thanks but i work in Basic and i'm clueless with Cto be honest I'm not sure what that code means apart from their being the assignment of variables and an IF/THEN/ELSE statement. Can you explain that in simple mathematical terms ?

author: armandas
author_rank: Frequent Contributor
timestamp: December 30, 2009, 06:02:14 pm
content: Read the article onLFSRas well. LFSR is damn simple, and MLS is just values generated by LFSR stored in a sequence. Which is what this complex-looking formula tells you:

author: Simon
author_rank: Unknown
timestamp: December 30, 2009, 06:11:03 pm
content: well the formula you posted from wikipedia is what bugged me but the other article that provides a more block diagram explanation makes more sense and somemthing I can use as inspiration

author: jahonen
author_rank: Super Contributor
timestamp: December 30, 2009, 06:58:04 pm
content: Well, my BASIC is somewhat rusty, but I'll try anyway.Code:Const IB1 = 1Const IB2 = 2Const IB5 = 16Const IB18 = 131072Const Mask = (IB1 + IB2 + IB5)Function irbit2(ByRef iseed As Long) As LongDim retval As LongIf iseed And IB18 Theniseed = ((iseed Xor Mask) * 2) Or IB1retval = 1Elseiseed = iseed * 2retval = 0End Ifirbit2 = retvalEnd FunctionThis is closest equivalent of the C code I could think of with my poor knowledge of BASIC (in fact I used MS Excel VBA to check the syntax atleast somewhat).Regards,Janne

author: charliex
author_rank: Frequent Contributor
timestamp: December 30, 2009, 11:47:25 pm
content: PicBasic has a RANDOM keyword.

author: Simon
author_rank: Unknown
timestamp: December 31, 2009, 07:14:36 am
content: I'm using Mikroe basic

author: Hopasaurus
author_rank: Newbie
timestamp: January 01, 2010, 09:12:42 pm
content: Make an array with about 20 elements with different numbers and loop through the array to get your time outs.This will be "random" enough for the party fun, by time anyone could figure out the sequence the game will be done.like this:const randomdata as byte  = (25,12,34,52,16,10,12,46,12,52,12,44,57,12,23,64,21,23,12)dim delaytime as longwordmain:for i = 0 to 20'delay i secondsdelaytime = randomdatadelaytime = delaytime * 1000Delay_ms(delaytime)'go beepnext igoto mainendI tried to make sure the code is close to right for Mikroe basic, you get to do the rest.If the whole array is too short or too long of a delay, just scale it by changing the multiply by 1000, then you do not have to change all the numbers, or better yet add a pot and have it adjustable during the party.Hope you have a fun party.

author: Simon
author_rank: Unknown
timestamp: January 01, 2010, 09:55:09 pm
content: hi, thanks for the input, I'll have to read up in my basic and see how to work out the larger project