Lottery Numbers Generator (Flowcode V4)

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8287
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2371 times
Been thanked: 3378 times
Contact:

Lottery Numbers Generator (Flowcode V4)

Postby medelec35 » Sun Jan 16, 2011 7:05 pm

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 :lol: :P
Attachments
Loto Number Generator 18F4455.fcf
(11 KiB) Downloaded 1129 times
These users thanked the author medelec35 for the post (total 4):
Benj (Mon Jan 17, 2011 10:20 am) • celebrium (Fri Sep 14, 2012 7:55 pm) • bercioiu (Wed Mar 13, 2013 12:27 am) • ionize (Wed May 07, 2014 9:54 pm)
Rating: 22.22%
 
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

User avatar
Benj
Matrix Staff
Posts: 14073
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4297 times
Been thanked: 4050 times
Contact:

Re: Lottery Numbers Generator (Flowcode V4)

Postby Benj » Mon Jan 17, 2011 10:21 am

Brilliant many thanks for this Medelec great idea, Do we have to pay out if we win with your numbers :)

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8287
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2371 times
Been thanked: 3378 times
Contact:

Re: Lottery Numbers Generator (Flowcode V4)

Postby medelec35 » Mon Jan 17, 2011 11:06 am

medelec35 wrote:but if you do use it and win, please give generously to the developer, say 1/2 - 1 million :lol: :P

Of course! :P Bankers draft is fine. I will settle for 10% :lol:
These users thanked the author medelec35 for the post:
Sparkoids (Sun Dec 07, 2014 6:52 pm)
Rating: 5.56%
 
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

User avatar
Benj
Matrix Staff
Posts: 14073
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4297 times
Been thanked: 4050 times
Contact:

Re: Lottery Numbers Generator (Flowcode V4)

Postby Benj » Mon Jan 17, 2011 1:00 pm

Sounds good to me :P I'll be sure to let you know :mrgreen:

Spanish_dude
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)

Postby Spanish_dude » Wed Feb 23, 2011 10:14 pm

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:

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);
 }
}


I must say it works pretty good :mrgreen: .

Regards,

Nicolas L. F.
These users thanked the author Spanish_dude for the post:
medelec35 (Thu Mar 03, 2011 9:02 am)
Rating: 5.56%
 

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8287
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2371 times
Been thanked: 3378 times
Contact:

Re: Lottery Numbers Generator (Flowcode V4)

Postby medelec35 » Thu Mar 03, 2011 9:01 am

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

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

Spanish_dude
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)

Postby Spanish_dude » Thu Mar 03, 2011 1:42 pm

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.

It's the same when you use the random functions in C on the PC without setting the "seed" in the srand function.

( http://www.cplusplus.com/reference/clib ... lib/srand/ )

allenf
Posts: 9
Joined: Thu Jul 14, 2011 5:56 pm
Contact:

Re: Lottery Numbers Generator (Flowcode V4)

Postby allenf » Fri Jul 15, 2011 1:21 am

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

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8287
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2371 times
Been thanked: 3378 times
Contact:

Re: Lottery Numbers Generator (Flowcode V4)

Postby medelec35 » Fri Jul 15, 2011 7:58 am

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

Are you generating your numbers with the simulator or real hardware?
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.

Im guessing it's just a bug, within the Flowcode simulator?

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

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

allenf
Posts: 9
Joined: Thu Jul 14, 2011 5:56 pm
Contact:

Re: Lottery Numbers Generator (Flowcode V4)

Postby allenf » Fri Jul 15, 2011 1:23 pm

not having a dig i just found it amusing sorry

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8287
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2371 times
Been thanked: 3378 times
Contact:

Re: Lottery Numbers Generator (Flowcode V4)

Postby medelec35 » Fri Jul 15, 2011 1:34 pm

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 :P )
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

User avatar
Benj
Matrix Staff
Posts: 14073
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4297 times
Been thanked: 4050 times
Contact:

Re: Lottery Numbers Generator (Flowcode V4)

Postby Benj » Fri Jul 15, 2011 4:23 pm

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.
These users thanked the author Benj for the post:
medelec35 (Fri Jul 15, 2011 4:50 pm)
Rating: 5.56%
 

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8287
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2371 times
Been thanked: 3378 times
Contact:

Re: Lottery Numbers Generator (Flowcode V4)

Postby medelec35 » Fri Jul 15, 2011 4:51 pm

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.

Nice 1!
Thanks Ben
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 64 times
Been thanked: 306 times
Contact:

Re: Lottery Numbers Generator (Flowcode V4)

Postby JonnyW » Mon Jul 18, 2011 9:47 am

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.
These users thanked the author JonnyW for the post:
medelec35 (Mon Jul 18, 2011 5:23 pm)
Rating: 5.56%
 

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8287
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2371 times
Been thanked: 3378 times
Contact:

Re: Lottery Numbers Generator (Flowcode V4)

Postby medelec35 » Mon Jul 18, 2011 5:22 pm

Thanks Jonny,
Simulation now works the same as the hardware.. perfect.

Martin
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

tijs
Posts: 7
Joined: Tue Mar 12, 2013 4:57 pm
Has thanked: 1 time
Been thanked: 1 time
Contact:

Re: Lottery Numbers Generator (Flowcode V4)

Postby tijs » Tue Mar 12, 2013 5:35 pm

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

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8287
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2371 times
Been thanked: 3378 times
Contact:

Re: Lottery Numbers Generator (Flowcode V4)

Postby medelec35 » Tue Mar 12, 2013 5:54 pm

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
Attachments
Loto Number Generator 16F616.fcf
Flowcode V5
(16.1 KiB) Downloaded 307 times
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

tijs
Posts: 7
Joined: Tue Mar 12, 2013 4:57 pm
Has thanked: 1 time
Been thanked: 1 time
Contact:

Re: Lottery Numbers Generator (Flowcode V4)

Postby tijs » Tue Mar 12, 2013 6:01 pm

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

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8287
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2371 times
Been thanked: 3378 times
Contact:

Re: Lottery Numbers Generator (Flowcode V4)

Postby medelec35 » Tue Mar 12, 2013 6:17 pm

[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
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

tijs
Posts: 7
Joined: Tue Mar 12, 2013 4:57 pm
Has thanked: 1 time
Been thanked: 1 time
Contact:

Re: Lottery Numbers Generator (Flowcode V4)

Postby tijs » Tue Mar 12, 2013 6:24 pm

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
Attachments
keuze ledjes1.msg.txt
the error file
(3.54 KiB) Downloaded 235 times
keuze ledjes1.fcf
flowcode V4 file
(25.45 KiB) Downloaded 246 times

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8287
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2371 times
Been thanked: 3378 times
Contact:

Re: Lottery Numbers Generator (Flowcode V4)

Postby medelec35 » Tue Mar 12, 2013 6:53 pm

Hi tijs,
Now compiles ok.
What was missing is:

Code: Select all

#include <rand.h>

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
Attachments
keuze ledjes2.fcf
(25.99 KiB) Downloaded 267 times
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

tijs
Posts: 7
Joined: Tue Mar 12, 2013 4:57 pm
Has thanked: 1 time
Been thanked: 1 time
Contact:

Re: Lottery Numbers Generator (Flowcode V4)

Postby tijs » Tue Mar 12, 2013 7:05 pm

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 .

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8287
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2371 times
Been thanked: 3378 times
Contact:

Re: Lottery Numbers Generator (Flowcode V4)

Postby medelec35 » Tue Mar 12, 2013 7:45 pm

Your welcome
tijs wrote: i hope its gonna gow into the pic

The beauty of Flowcode is you can very easily find out first by compiling to Hex, and having a look at the compiler messages.

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

Which is from flowchart I modified for you.

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


So you will know before compiling to chip.

Martin
These users thanked the author medelec35 for the post:
tijs (Tue Mar 12, 2013 7:51 pm)
Rating: 5.56%
 
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

tijs
Posts: 7
Joined: Tue Mar 12, 2013 4:57 pm
Has thanked: 1 time
Been thanked: 1 time
Contact:

Re: Lottery Numbers Generator (Flowcode V4)

Postby tijs » Tue Apr 16, 2013 5:50 pm

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

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8287
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2371 times
Been thanked: 3378 times
Contact:

Re: Lottery Numbers Generator (Flowcode V4)

Postby medelec35 » Tue Apr 16, 2013 6:23 pm

Not sure on what your problem is?
When I looked at your flowchart I did notice that you left

Code: Select all

#include <rand.h>
out
See above post: viewtopic.php?f=26&t=8025#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

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.