Author Topic: "random" generator algorithm for pic  (Read 10415 times)

0 Members and 1 Guest are viewing this topic.

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18908
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
"random" generator algorithm for pic
« on: December 30, 2009, 03:40:29 pm »
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
« Last Edit: December 30, 2009, 03:49:33 pm by Simon »
 

Offline jahonen

  • Super Contributor
  • ***
  • Posts: 1057
  • Country: fi
Re: "random" generator algorithm for pic
« Reply #1 on: December 30, 2009, 03:56:40 pm »
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
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18908
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: "random" generator algorithm for pic
« Reply #2 on: December 30, 2009, 04:21:39 pm »
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)
« Last Edit: December 30, 2009, 04:32:33 pm by Simon »
 

Offline jahonen

  • Super Contributor
  • ***
  • Posts: 1057
  • Country: fi
Re: "random" generator algorithm for pic
« Reply #3 on: December 30, 2009, 05:42:13 pm »
Math is great fogscreen for making simple things look much more complex :) Numerical Recipes in C gives easy C implementation (this example for 2^18-1 bits long random sequence):

Code: [Select]
#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
« Last Edit: December 30, 2009, 05:44:31 pm by jahonen »
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18908
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: "random" generator algorithm for pic
« Reply #4 on: December 30, 2009, 06:00:33 pm »
Thanks but i work in Basic and i'm clueless with C

to 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 ?
 

Offline armandas

  • Frequent Contributor
  • **
  • Posts: 336
  • Country: jp
    • My projects
Re: "random" generator algorithm for pic
« Reply #5 on: December 30, 2009, 06:02:14 pm »
Read the article on LFSR as 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:

 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18908
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: "random" generator algorithm for pic
« Reply #6 on: December 30, 2009, 06:11:03 pm »
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
 

Offline jahonen

  • Super Contributor
  • ***
  • Posts: 1057
  • Country: fi
Re: "random" generator algorithm for pic
« Reply #7 on: December 30, 2009, 06:58:04 pm »
Well, my BASIC is somewhat rusty, but I'll try anyway.

Code: [Select]

Const IB1 = 1
Const IB2 = 2
Const IB5 = 16
Const IB18 = 131072
Const Mask = (IB1 + IB2 + IB5)

Function irbit2(ByRef iseed As Long) As Long

Dim retval As Long

  If iseed And IB18 Then
    iseed = ((iseed Xor Mask) * 2) Or IB1
    retval = 1
  Else
    iseed = iseed * 2
    retval = 0
  End If
  irbit2 = retval

End Function

This 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
« Last Edit: December 30, 2009, 07:03:34 pm by jahonen »
 

Offline charliex

  • Frequent Contributor
  • **
  • Posts: 346
  • Country: 00
  • Car Hacker
Re: "random" generator algorithm for pic
« Reply #8 on: December 30, 2009, 11:47:25 pm »
PicBasic has a RANDOM keyword.
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18908
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: "random" generator algorithm for pic
« Reply #9 on: December 31, 2009, 07:14:36 am »
I'm using Mikroe basic
 

Offline Hopasaurus

  • Newbie
  • Posts: 4
Re: "random" generator algorithm for pic
« Reply #10 on: January 01, 2010, 09:12:42 pm »
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 [20] = (25,12,34,52,16,10,12,46,12,52,12,44,57,12,23,64,21,23,12)
dim delaytime as longword

main:

for i = 0 to 20
'delay i seconds
delaytime = randomdata
delaytime = delaytime * 1000
Delay_ms(delaytime)

'go beep
next i


goto main
end

I 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.
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18908
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: "random" generator algorithm for pic
« Reply #11 on: January 01, 2010, 09:55:09 pm »
hi, thanks for the input, I'll have to read up in my basic and see how to work out the larger project
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->