PIC Random Nunber

Use this section to discuss your embedded Flowcode projects.
Post Reply
jay_dee
Posts: 135
http://meble-kuchenne.info.pl
Joined: Sun Dec 20, 2020 6:06 pm
Has thanked: 47 times
Been thanked: 31 times

PIC Random Nunber

Post by jay_dee »

Hi, this one has me stumped.
Is there a way to create a psudo random number, between limits in flowcode or via a C call?
I wish to create a delay with a random length between 0.5 and 4 seconds.
J.
Im using a PIC 18F 2585

mnfisher
Valued Contributor
Posts: 1003
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 106 times
Been thanked: 519 times

Re: PIC Random Nunber

Post by mnfisher »

Flowcode had random() and you could use something like:

500+(random() % 3500) to generate time between 500 and 4000 then delay in ms...

Martin

chipfryer27
Valued Contributor
Posts: 1191
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 289 times
Been thanked: 418 times

Re: PIC Random Nunber

Post by chipfryer27 »

Hi

Generally speaking, yes, you can.

If you do a search in the forum you will find pleanty of info such as this

https://www.matrixtsl.com/mmforums/view ... 97#p106397

You might also want to include a truly random variable such as a temperature reading or an ADC on a floating pin to help mix it up :)

Regards

EDIT...

I see Martinn beat me to it :)

jay_dee
Posts: 135
Joined: Sun Dec 20, 2020 6:06 pm
Has thanked: 47 times
Been thanked: 31 times

Re: PIC Random Nunber

Post by jay_dee »

Ah Thanks Guys.
argh flipping heck..yeah found it now! Thanks Martin. :)
I did several searches on the current FC forum and did not find anything, FC is great on some of its features these days but sometimes even the simplest things are darn hard to find.

chipfryer27
Valued Contributor
Posts: 1191
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 289 times
Been thanked: 418 times

Re: PIC Random Nunber

Post by chipfryer27 »

Hi

The "old" forum is a great source of knowledge going back to stoneage times :)

Regards

mnfisher
Valued Contributor
Posts: 1003
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 106 times
Been thanked: 519 times

Re: PIC Random Nunber

Post by mnfisher »

The issue with Random() is that the resultant sequence will be the same on every run. Great idea from Iain to use a 'genuinely random number (possibly as a seed) - for example current time in ms or a floating pin..

You can do this with a C Block and

srand(FCL_SEED);

Where .seed is the value you want to use as a seed (so something from an ADC etc)

Using the same seed is good for testing purposes - you get the same sequence every time - but may not be so good for games or other programs that rely on the user not knowing what is going to happen..

jay_dee
Posts: 135
Joined: Sun Dec 20, 2020 6:06 pm
Has thanked: 47 times
Been thanked: 31 times

Re: PIC Random Nunber

Post by jay_dee »

Fantastic, thanks. I have the standard random function working well, I'll try the srand C code tomorrow. :)

Post Reply