Random Number Generation

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 5.
To post in this forum you must have a registered copy of Flowcode 5 or higher.

Moderator: Benj

Post Reply
User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times
Contact:

Random Number Generation

Post by acestu »

Hi,

I have just put this code together for a Lottery Number Generator but the random number calculation is wrong, could somebody give me some pointers please, I am enclosing the code.

Thanks
Acestu
lotto.fcf
(17.91 KiB) Downloaded 319 times
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Random Number Generation

Post by medelec35 »

Hi Acestu,
I did a similar Lottery generator here:
http://www.matrixmultimedia.com/mmforum ... 885#p22091

Maybe that will help.

Martin
Martin

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times
Contact:

Re: Random Number Generation

Post by acestu »

Thanks Martin,

I have already seen your lotto thread, in fact it is what prompted me to have a go at doing a flowchart from scratch, basically what I was trying to do was to generate the first random number between 1 and 49 then the second number generate a random number between 1 and 49 minus the first generated number and so on, but I think the formula literally subtracts the number instead of just not picking it again, if that makes sense.

I suppose what I am trying to say in computer terms is " I have an array of numbers 1 to 49 , take a number away and show it, take a number away and show it and so on "

Although this job sounds easy it is also easy to get wrong because it could simulate somebody taking a ball out of the machine ok, then the next ball if it is the same throwing it back in the machine instead of leaving it out.

Surely there must be 1 line of formula that could do this...or is there not

Thanks
Acestu
Last edited by acestu on Sat Nov 17, 2012 10:20 am, edited 1 time in total.
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Random Number Generation

Post by medelec35 »

acestu wrote:Thanks Martin,
I think the formula literally subtracts the number instead of just not picking it again, if that makes sense.
Yes that does make sense.
What I believe is happening is your subtracting numbers rather than removing them.

Because they are being subtracted and they are bytes, they are rolling under.
E.g 1 - 3 = 254

If you wanted to create a lottery generator that works differently to the one I posted, then how about using a 49 byte array.
generate a random number between 1 and 49. e.g 10. Check array 10, if <> 0 then generate a new random number.
Else If array 10 = 0, assign array 10 with 10 then display the number 10 etc.

Would you like help with that, or do you want to have a go yourself for learning purposes?

Martin
Martin

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times
Contact:

Re: Random Number Generation

Post by acestu »

Sorry Martin,

I have just edited my post before I saw this post, I will have a look at this way then post back.

thanks
Acestu
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times
Contact:

Re: Random Number Generation

Post by acestu »

Hi Martin,

I have changed the Flowchart to how I think it should work ie create the Array of integer numbers between 1 and 49 and then I have put at each number section what I would like to do, unfortunately I have no idea how to put it in formula/flowcode terms, if you could help me with this I would be grateful..

Thanks
Acestu
lotto_2.fcf
(18.96 KiB) Downloaded 345 times
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Random Number Generation

Post by medelec35 »

Sure, I have done that for you.
Can you please try attached Flowchart, and let me know how it goes.
Thanks.

Martin

Edit just had a look at your program.
To create an array (A single variable name that can be assigned with multiple values)
Just create variable as normal and at the end of the variable place amount of multiple variables required within squared brackets.
E.g you want to assign a variable called random_Num with 49 different numbers then add the following byte variable:

random_Num[50]

Reason its one higher is because random_Num[0] is the 1st variable location used and that's not used for storage.
Attachments
lotto3.fcf
(18.5 KiB) Downloaded 303 times
Martin

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: Random Number Generation

Post by kersing »

medelec35 wrote: To create an array (A single variable name that can be assigned with multiple values)
Just create variable as normal and at the end of the variable place amount of multiple variables required within squared brackets.
E.g you want to assign a variable called random_Num with 49 different numbers then add the following byte variable:

random_Num[48]

Reason its one less is because random_Num[0] is the 1st variable location used.
Martin,

I've read this a number of times and it seems to suggest you need to use random_Num[48] to create an array of size 49. That is not true. You need to use random_Num[49] to create an array with 49 elements. The first element will be arr[0] and the last (49th) element will be random_Num[48]. Nasty things might happen if you declare an array to small. (C is not particularly well known for its bounds checking, meaning there is none. Flowcode does have bounds checking but only when entering the array index directly or when simulating while using variables)

Regards,

Jac
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Random Number Generation

Post by medelec35 »

Hi Jac,
Your indeed correct.
I actually used [50] for the lottery generator.

The number[48] was an error on my part I meant to go the other way as in higher and not lower :oops:

I will reword my post as it will confusing.

Thanks for pointing it out.

Since I have not got a 4 line display to test generator, I have converted generator to run on a MIAC.
I have also added a small timer 0 routine so the random numbers are totally random each time power is applied.

Martin
Attachments
lotto3 miac.fcf
(20.17 KiB) Downloaded 297 times
Martin

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times
Contact:

Re: Random Number Generation

Post by acestu »

Hi Martin,

Thanks for your code I think it will take me a while to work out how it works though, when you say you have used a timer so that the numbers are random after a power reset, why is this ?

thanks
Acestu
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Random Number Generation

Post by medelec35 »

Random is not as random as you think.

Every time you connect power for the 1st time, you will find the random numbers will be the same sequence of numbers every time

So the timer interrupt was used generate a random number. e.g 128.
Then using this number a loop counted 128 times, and using

Code: Select all

Random_Number = (random () MOD 49) + 1 
( could have used any random range e.g Random_Number = (random () MOD 150) )
So the random function was accessed 128 times before the true Random_Number = (random () MOD 49) + 1 function was used.

Next time timer0 could have generated 239 after power is 1st applied. this means that Random function is more random than if not used timer0 interrupt.
Where you see:

Code: Select all

FCV_TIMER0=tmr0l;
This just means retrieve value stored in timer0 low register, and assign it to a Flowcode variable called timer0.

Martin
Martin

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times
Contact:

Re: Random Number Generation

Post by acestu »

Thats great Martin because that was going to be my question ie. how random is the random function,

I have forgotten to put the back light switch in the code, if I have a push button how would I work it so that when I pushed it the back light would come on and when I pushed it again the back light would go off, I suppose you would have to have some sort of "switch to the opposite condition function" wouldn't you ?
also it would have to go at the beginning of the code wouldn't it ?

Thanks
Acestu
lotto4.fcf
(20.89 KiB) Downloaded 284 times
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Random Number Generation

Post by medelec35 »

Hi Acestu,
A way of creating the opposite function is =! (equals NOT)
So you could have:

Code: Select all

Backlight = !Backlight
What I have done is changed your switch connection to port B7 which is set up as port B7 interrupt.

So as soon as B7 changes state, port b interrupt macro is triggered.

Then if switch is then read, back light will change state each time switch is read as high.

The good thing about using this port B interrupt method is: No matter at what part of code is currently accessed, when switch goes high, then code is paused, back light is toggles, then code carries on from where it was paused from.

If its not working as expected then I will build in some more switch debounce.

Martin
Attachments
lotto5.fcf
(23.88 KiB) Downloaded 216 times
Martin

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times
Contact:

Re: Random Number Generation

Post by acestu »

Thanks Martin

That works brilliantly, I have not yet come across interrupts as I am trying to learn programming from Flowcode and have little knowledge of C etc....

Does the interrupt mean that any input on the B port will interrupt the flowchart even though it could be looping ?

Thanks
Acestu
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Random Number Generation

Post by Enamul »

Does the interrupt mean that any input on the B port will interrupt the flowchart even though it could be looping ?
Actually interrupt is useful when processor is doing something but you want to draw it's attention to do something else. The main loop of the code is skipped rather it executes a defined interrupt service routine (ISR) which is a macro in Flowcode..after that it returns to its main program flow.
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times
Contact:

Re: Random Number Generation

Post by acestu »

Thanks Enamul,

I often wondered how this was achieved....

Thanks
Acestu
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Random Number Generation

Post by Enamul »

If you want I can post you simple RB0/INT and TRM0 int test code to get some idea about those..
Enamul
University of Nottingham
enamul4mm@gmail.com

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Random Number Generation

Post by medelec35 »

Your welcome mate.
acestu wrote: Does the interrupt mean that any input on the B port will interrupt the flowchart even though it could be looping ?
It depends of the chip and the settings.
For instance some devices have got an ioc register which means you can set to interrupt on individual port B pins. You will know when you can do that with flowcode since the properties on the interrupt will not be grayed out.

If properties on the interrupt are grayed out, then if any of port B pins that are available for port interrtpts change state, then code is interrupted to run interrupt macro.
You MUST make sure the macro is exited out the bottom, since while interrupt macro is accessed, then all other interrupts will be disabled, until macro is exited normally. You cant use a call macro within any interrupt macros.

Older devices uses port interrpt on B4 to B7.
Whereas newer devices use B0 to B7.
(at least I belive that to be the case, but I could be wrong).

Some devices don't have a port B e.g 16F616.
So they have interrupt on portA.
You can find out what pins are used for ioc (Interrupt On Change) by loading the devices data sheet and search for ioc.

Have you tried the Flowcode beginners course which can be found in the learning center?
See:
http://www.matrixmultimedia.com/lc_index.php

Martin

Edit: three posts was posted in the time it took me to type a reply.
Boy am I slow at typing. :lol:
Martin

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times
Contact:

Re: Random Number Generation

Post by acestu »

Yes please Enamul, the more info I have the better...

Thanks
Acestu
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times
Contact:

Re: Random Number Generation

Post by acestu »

Thanks for that info Martin, I will have a look at the courses now...

Thanks
Acestu

P.S. your typing can't be any worse than mine lol :D
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Random Number Generation

Post by Enamul »

Hi
Here is a basic example for RB0 INT...
Count will increase count and it will be shown in PORTC>LED. If you press RB0 it will causes count to reset to 0.
I have posted a detailed explanation of TMR0 and RB0 both interrupt present in a code in the following thread..
http://www.matrixmultimedia.com/mmforum ... 29&t=11196
Attachments
1. RB0 interrupts.FCF
(10 KiB) Downloaded 203 times
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times
Contact:

Re: Random Number Generation

Post by acestu »

Hi Martin,

The chip I have selected for this project just happened to be lying around but I think it is way over the top with the Can bus etc, if I was to look for a cheaper / smaller chip would I be right in thinking that I need a device ports for the LCD and switches and a timer and an interupt on port B for the backlight feature ?

Thanks
Acestu
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times
Contact:

Re: Random Number Generation

Post by acestu »

I have changed the device to a pic16f628 and it seems to be ok

Thanks
Acestu
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Random Number Generation

Post by medelec35 »

Hi Acestu,

If you would like a very cheap microcontroller with low pin count, then you could try 16F616.
I have created a flowchart that device.
Instead of ioc which uses port C (16F616), I have used INT (rising or falling edge interrupt) which uses RA2
Note, RA2 interrupt will not simulate in Flowcode, but will work with hardware.

Rather than connecting an external crystal, you use 8MHz internal oscillator.

I don't know if your aware that you can use internal weak pull-ups.
Think of these as weak internal pull-up resistors connected between port A pins and VCC.

I have enabled weak pull-ups on RA0, RA1 & RA2. These are pins the three switches are connected to.
Therefore you do not need to use external resistors to pull-up switches anymore.
The only difference is the switches are active low.
So the pins are +5V until switch is pressed, then voltage across pin will drop to 0V
The flowchart and hardware can be changed back again, but I thought will show you some different options.


You wanted to know why I used:
lotto loop.png
(3.67 KiB) Downloaded 7353 times
Since the loto generator generates a random number, it look to see if the array of the generated number = 0 or not.
If its = 0 then random number has not been used. If it is not = 0 then random number has already been used.

The problem with microcontrollers is when power is first applied, all the variables could contain a random number.
That is why you must always write to a variable before it is read for the first time.
That loop just writes a 0 to all the Random_Store arrays.
So count = 0
Random_Store[0] = 0
Count = Count + 1
So Random_Store[1] = 0
.......
.......
Random_Store[49] = 0


Martin
Attachments
lotto6.fcf
(23.36 KiB) Downloaded 232 times
Martin

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times
Contact:

Re: Random Number Generation

Post by acestu »

Hi Martin,

Thanks for the info, very interesting and those devices sound cheap as chips lol, I will get some ordered and have a play with that code...

Thanks again
Acestu
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

Post Reply