Page 1 of 1
PIC Random Nunber
Posted: Thu May 02, 2024 7:27 pm
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
Re: PIC Random Nunber
Posted: Thu May 02, 2024 7:31 pm
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
Re: PIC Random Nunber
Posted: Thu May 02, 2024 7:36 pm
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

Re: PIC Random Nunber
Posted: Thu May 02, 2024 8:06 pm
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.
Re: PIC Random Nunber
Posted: Thu May 02, 2024 8:31 pm
by chipfryer27
Hi
The "old" forum is a great source of knowledge going back to stoneage times
Regards
Re: PIC Random Nunber
Posted: Thu May 02, 2024 8:53 pm
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..
Re: PIC Random Nunber
Posted: Thu May 02, 2024 10:04 pm
by jay_dee
Fantastic, thanks. I have the standard random function working well, I'll try the srand C code tomorrow.
