Lottery Numbers Generator (Flowcode V4)
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Lottery Numbers Generator (Flowcode V4)
This basic Flowchart will generate 6 random numbers between 1 and 49.
It should never repeat the same number within the 6 generated numbers. If same number is generated within the six, there is a routine to reject it and re-pick another number.
If you want to choose a different set of numbers, then press switch connected to PortA0
Target device can be easily changed. There is a note about this on the flowchart.
There are three issues with generating random numbers.
1) When using the format: Random_Number = (random()MOD 49)+1
When running simulator, numbers showing are between 1 and 255. On hardware numbers generated are between 1 and 49.
(Flowcode V4 only for a fix see JonnyW post below. Fixed with Flowcode V5)
2) On power up, numbers always start the same. So every time you reset, first number could always be 41, 2nd could be 26 etc.
I have got a way round this but there are probably better methods available. I have used the random() function within a loop that is determined by reading value of timer0 register. My theory is timer0 reg will increase by 1 every 4 clock cycles. since delays are not 100% precise, by the time the value of timer0 is read, in theory will have a different value each time. Hence random() will be run several times before actual lottery numbers are generated. So numbers should always be different on start-up.
3) If using random() function:
#include <rand.h> should be placed in Supplementary code window. ('View' 'Project Options' & click on 'Use Supplementary code')
This project is just for a bit of fun, but if you do use it and win, please give generously to the developer, say 1/2 - 1 million
It should never repeat the same number within the 6 generated numbers. If same number is generated within the six, there is a routine to reject it and re-pick another number.
If you want to choose a different set of numbers, then press switch connected to PortA0
Target device can be easily changed. There is a note about this on the flowchart.
There are three issues with generating random numbers.
1) When using the format: Random_Number = (random()MOD 49)+1
When running simulator, numbers showing are between 1 and 255. On hardware numbers generated are between 1 and 49.
(Flowcode V4 only for a fix see JonnyW post below. Fixed with Flowcode V5)
2) On power up, numbers always start the same. So every time you reset, first number could always be 41, 2nd could be 26 etc.
I have got a way round this but there are probably better methods available. I have used the random() function within a loop that is determined by reading value of timer0 register. My theory is timer0 reg will increase by 1 every 4 clock cycles. since delays are not 100% precise, by the time the value of timer0 is read, in theory will have a different value each time. Hence random() will be run several times before actual lottery numbers are generated. So numbers should always be different on start-up.
3) If using random() function:
#include <rand.h> should be placed in Supplementary code window. ('View' 'Project Options' & click on 'Use Supplementary code')
This project is just for a bit of fun, but if you do use it and win, please give generously to the developer, say 1/2 - 1 million
- Attachments
-
- Loto Number Generator 18F4455.fcf
- (11 KiB) Downloaded 1487 times
Martin
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Lottery Numbers Generator (Flowcode V4)
Brilliant many thanks for this Medelec great idea, Do we have to pay out if we win with your numbers
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- 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: Lottery Numbers Generator (Flowcode V4)
Of course! Bankers draft is fine. I will settle for 10%medelec35 wrote:but if you do use it and win, please give generously to the developer, say 1/2 - 1 million
Martin
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Lottery Numbers Generator (Flowcode V4)
Sounds good to me I'll be sure to let you know
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Posts: 594
- Joined: Thu Sep 17, 2009 7:52 am
- Location: Belgium
- Has thanked: 63 times
- Been thanked: 102 times
- Contact:
Re: Lottery Numbers Generator (Flowcode V4)
Hi,
I came up with another way on how to make random numbers that changes on every start-up / reset.
So my idea is to use the EEPROM, as I never use it and has random values stored.
Take the first two values of the EEPROM (address 0 & address 1), store them in, lets say, eeprom0 and eeprom1.
Then make an integer of those two bytes : eeprom0 << 8 | eeprom1
And then put that integer as argument in the srand() function : srand( eeprom0 << 8 | eeprom1 )
The next thing you'll need to do is change the EEPROM value on address 0 & 1.
To do that, write on address 0 & 1 of the EEPROM a random number from 0 to 255 : rand() % 256
That's it, now you can use rand() % MAX + MIN for your random numbers.
Here's an example I made with the mikroC, as I had some problems when using rand and srand in Flowcode:
I must say it works pretty good .
Regards,
Nicolas L. F.
I came up with another way on how to make random numbers that changes on every start-up / reset.
So my idea is to use the EEPROM, as I never use it and has random values stored.
Take the first two values of the EEPROM (address 0 & address 1), store them in, lets say, eeprom0 and eeprom1.
Then make an integer of those two bytes : eeprom0 << 8 | eeprom1
And then put that integer as argument in the srand() function : srand( eeprom0 << 8 | eeprom1 )
The next thing you'll need to do is change the EEPROM value on address 0 & 1.
To do that, write on address 0 & 1 of the EEPROM a random number from 0 to 255 : rand() % 256
That's it, now you can use rand() % MAX + MIN for your random numbers.
Here's an example I made with the mikroC, as I had some problems when using rand and srand in Flowcode:
Code: Select all
/** LCD connections (same as in flowcode) **/
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
/** Global Variables **/
short random_number = 0;
short eeprom_data[2] = {0};
void main()
{
// Set PORT direction
TRISB = 0;
// Set PORT value
PORTB = 0;
eeprom_data[0] = EEPROM_Read(0);
eeprom_data[1] = EEPROM_Read(1);
srand(eeprom[0] << 8 | eeprom[1]);
EEPROM_Write(0, rand() % 256);
EEPROM_Write(1, rand() % 256);
Lcd_Init();
Lcd_Out(2, 1, "LCD Test");
while (1)
{
random_number = rand() % 10;
Lcd_Chr(1, 1, random_number + '0');
Delay_ms(2000);
}
}
Regards,
Nicolas L. F.
-
- 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: Lottery Numbers Generator (Flowcode V4)
Thanks Nicolas. I will have a play, and see what results are.
It's odd that random is not actually random, if chip is reset.
Its always the same set of numbers.
It's odd that random is not actually random, if chip is reset.
Its always the same set of numbers.
Martin
-
- Posts: 594
- Joined: Thu Sep 17, 2009 7:52 am
- Location: Belgium
- Has thanked: 63 times
- Been thanked: 102 times
- Contact:
Re: Lottery Numbers Generator (Flowcode V4)
It's the same when you use the random functions in C on the PC without setting the "seed" in the srand function.medelec35 wrote:Thanks Nicolas. I will have a play, and see what results are.
It's odd that random is not actually random, if chip is reset.
Its always the same set of numbers.
( http://www.cplusplus.com/reference/clib ... lib/srand/ )
Re: Lottery Numbers Generator (Flowcode V4)
Aint never gonna win
numbers it came up with
46
26
215
17
248
45
See the problem LOL lottery only goes up to 49
numbers it came up with
46
26
215
17
248
45
See the problem LOL lottery only goes up to 49
-
- 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: Lottery Numbers Generator (Flowcode V4)
Are you generating your numbers with the simulator or real hardware?allenf wrote:Aint never gonna win
numbers it came up with
46
26
215
17
248
45
See the problem LOL lottery only goes up to 49
Im guessing it's just a bug, within the Flowcode simulator?medelec35 wrote: 1) When using the format: Random_Number = (random()MOD 49)+1
When running simulator, numbers showing are between 1 and 255. On hardware numbers generated are between 1 and 49.
I did try the Lottery Generator on hardware before posting file, and I can confirm numbers displayed on LCD are between 1 - 49.
Hope this helps.
Martin
Martin
-
- 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: Lottery Numbers Generator (Flowcode V4)
No problem,and no need to apologise as I did not take it as a dig at all, I just assumed you misread the post.
Of course your correct, if using the simulator you've not no chance (not that you got much chance anyway )
Of course your correct, if using the simulator you've not no chance (not that you got much chance anyway )
Martin
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Lottery Numbers Generator (Flowcode V4)
Hm that's interesting I would have thought that would be working, I will see if we can find the cause of that bug in the simulation and get it fixed.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- 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: Lottery Numbers Generator (Flowcode V4)
Nice 1!Benj wrote:Hm that's interesting I would have thought that would be working, I will see if we can find the cause of that bug in the simulation and get it fixed.
Thanks Ben
Martin
- JonnyW
- Posts: 1230
- Joined: Fri Oct 29, 2010 9:13 am
- Location: Matrix Multimedia Ltd
- Has thanked: 63 times
- Been thanked: 290 times
- Contact:
Re: Lottery Numbers Generator (Flowcode V4)
Hi Martin. If it helps, this wont be an issue in v5 due to greater signed/unsigned control but for now a small mod to the program would fix this.
We suspect the issue is that the random() call on the PC is being pushed into a signed 32-bit value. This is then cast to a float (as all v4 simulated values are) which is still -ve. Performing (-ve MOD +ve) is still -ve, and this is then cropped to a byte, resulting in 'large' 200+ values being shown on occasion.
If in the code you do: (random() & 0xFF) MOD 49 instead, hopefully this will go away.
We suspect the issue is that the random() call on the PC is being pushed into a signed 32-bit value. This is then cast to a float (as all v4 simulated values are) which is still -ve. Performing (-ve MOD +ve) is still -ve, and this is then cropped to a byte, resulting in 'large' 200+ values being shown on occasion.
If in the code you do: (random() & 0xFF) MOD 49 instead, hopefully this will go away.
-
- 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: Lottery Numbers Generator (Flowcode V4)
Thanks Jonny,
Simulation now works the same as the hardware.. perfect.
Martin
Simulation now works the same as the hardware.. perfect.
Martin
Martin
Re: Lottery Numbers Generator (Flowcode V4)
i made a program in V4 but i can't send it to my pic its the 16F676 and i'm searching for another pic with 14 pins that supports the random() function
can anyone help me
can anyone help me
-
- 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: Lottery Numbers Generator (Flowcode V4)
Hi tijs,
I have converted Lottery Generator to work on a 16F616 target device which I believe is pin for pin compatible.
Plus 16F616 are much better and nearly 1/2 the price of 16F676.
This is untested on hardware, I have just modifed the Flowchart slightly.
What Flowcode version have you got as I have save with Flowcode V5.
If you have Flowcode V4 then I will post a V4 version for you.
Martin
I have converted Lottery Generator to work on a 16F616 target device which I believe is pin for pin compatible.
Plus 16F616 are much better and nearly 1/2 the price of 16F676.
This is untested on hardware, I have just modifed the Flowchart slightly.
What Flowcode version have you got as I have save with Flowcode V5.
If you have Flowcode V4 then I will post a V4 version for you.
Martin
- Attachments
-
- Loto Number Generator 16F616.fcf
- Flowcode V5
- (16.1 KiB) Downloaded 505 times
Martin
Re: Lottery Numbers Generator (Flowcode V4)
its for my own program its a kind of a game and there's a random in it but when i want to send it to the 16F676 it gives errors can the 16f616 read those randoms??
it's programmed in V4 so what could i do to make it work or do i actually need a new pic
anyway
thanks for al the help
it's programmed in V4 so what could i do to make it work or do i actually need a new pic
anyway
thanks for al the help
-
- 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: Lottery Numbers Generator (Flowcode V4)
[quote="tijs" when i want to send it to the 16F676 it gives errors can the 16f616 read those randoms??
[/quote]
I believe it's more the compiler that allows the code rather than the target device its self.
If you post your flowchart, I can take a look at it for you, and see what we can sort out.
You never know, may be able to get it working for you on 16F676.
But that device has very little memory
Martin
[/quote]
I believe it's more the compiler that allows the code rather than the target device its self.
If you post your flowchart, I can take a look at it for you, and see what we can sort out.
You never know, may be able to get it working for you on 16F676.
But that device has very little memory
Martin
Martin
Re: Lottery Numbers Generator (Flowcode V4)
tis is the file its programmed in v4 with 16f676 see what you can do
it would be of great help
it's sort of a memory game xp
i also have put the error fil in it maybe this can help you
thanks
it would be of great help
it's sort of a memory game xp
i also have put the error fil in it maybe this can help you
thanks
- Attachments
-
- keuze ledjes1.msg.txt
- the error file
- (3.48 KiB) Downloaded 417 times
-
- keuze ledjes1.fcf
- flowcode V4 file
- (25.45 KiB) Downloaded 433 times
-
- 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: Lottery Numbers Generator (Flowcode V4)
Hi tijs,
Now compiles ok.
What was missing is:
Within supplementary code
('View,'Project options') Click supplementary Code...
#include <rand.h> should be in the top window.
I have not tested your flowchart, just made sure it compiles ok.
Martin
Now compiles ok.
What was missing is:
Code: Select all
#include <rand.h>
('View,'Project options') Click supplementary Code...
#include <rand.h> should be in the top window.
I have not tested your flowchart, just made sure it compiles ok.
Martin
- Attachments
-
- keuze ledjes2.fcf
- (25.99 KiB) Downloaded 440 times
Martin
Re: Lottery Numbers Generator (Flowcode V4)
thank you very much tomorrow i hope its gonna gow into the pic
i thought that was the problem xp
I'l let you know
thank you very much .
i thought that was the problem xp
I'l let you know
thank you very much .
-
- 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: Lottery Numbers Generator (Flowcode V4)
Your welcome
Eg. This means it will compile OK and will fit in your target device:
Which is from flowchart I modified for you.
This means code is too big to fit in to your target device:
So you will know before compiling to chip.
Martin
The beauty of Flowcode is you can very easily find out first by compiling to Hex, and having a look at the compiler messages.tijs wrote: i hope its gonna gow into the pic
Eg. This means it will compile OK and will fit in your target device:
Code: Select all
Building CASM file
Memory Usage Report
===================
RAM available:64 bytes, used:55 bytes (86.0%), free:9 bytes (14.0%),
Heap size:9 bytes, Heap max single alloc:8 bytes
ROM available:1024 words, used:658 words (64.3%), free:366 words (35.7%)
success
Return code = 0
FINISHED
This means code is too big to fit in to your target device:
Code: Select all
Building CASM file
Memory Usage Report
===================
RAM available:64 bytes, used:58 bytes (90.7%), free:6 bytes (9.3%),
Heap size:6 bytes, Heap max single alloc:5 bytes
ROM available:1024 words, used:1155 words (112.7%), free:-131 words (-12.-7%)
Too much code to fit in ROM, overfilled by:131 locations.
failure
Return code = -2
Flowcode was unable to assemble the ASM file due to the following errors:
FINISHED
Martin
Martin
Re: Lottery Numbers Generator (Flowcode V4)
HI
its tijs from the random problem xp
i've programmed my pic an my program fits
but can it be that the 16F676 can't read the random function
do you know what the problem could be?
or can you tell me what pic with 14 pins can read that function.
thanks
tijs
its tijs from the random problem xp
i've programmed my pic an my program fits
but can it be that the 16F676 can't read the random function
do you know what the problem could be?
or can you tell me what pic with 14 pins can read that function.
thanks
tijs
-
- 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: Lottery Numbers Generator (Flowcode V4)
Not sure on what your problem is?
When I looked at your flowchart I did notice that you leftout
See above post: http://www.matrixmultimedia.com/mmforum ... 025#p46017
What other problem have you got?
E.g not compiling (if so can you post your flowchart please)
Or
Compiling OK but not running on hardware?
Can you also post the text file that contains compiler messages e.g keuze ledjes2.msg.txt
Please post as much information as you can, so I can help you further.
When I looked at your flowchart I did notice that you left
Code: Select all
#include <rand.h>
See above post: http://www.matrixmultimedia.com/mmforum ... 025#p46017
What other problem have you got?
E.g not compiling (if so can you post your flowchart please)
Or
Compiling OK but not running on hardware?
Can you also post the text file that contains compiler messages e.g keuze ledjes2.msg.txt
Please post as much information as you can, so I can help you further.
Martin