Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Random numbers for my Dice project

Status
Not open for further replies.

aljamri

Member
Hi'

I've tried my luck to do an electronics Dice and got it, but faced a problem could not overcome. The generated Random numbers had the same sequence every time I restart the circuit ( 4,4,2,6,3,1,1,4,3,6,3 .... ). Read somewhere that using srand() makes some difference, but when used it, and error message appeared. It is clear that I don't know how to use it. Any body does?

Following is the code:

Code:
unsigned short (a);
int rand(void);
      delay1;
      delay2;

int main(){
      delay1= 1000;
      delay2= 200;
      TRISA = 0b11110000; // RA5 as input (SWT)
      TRISB = 0b00000000; // PORTB Output

 // ********* Start of LED Test ********
Test:
      PORTB = 0b00000001; Vdelay_ms(delay2);
      PORTB = 0b00000010; Vdelay_ms(delay2);
      PORTB = 0b00000100; Vdelay_ms(delay2);
      PORTB = 0b00001000; Vdelay_ms(delay2);
      PORTB = 0b00010000; Vdelay_ms(delay2);
      PORTB = 0b00100000; Vdelay_ms(delay2);
      PORTB = 0b01000000; Vdelay_ms(delay2);
      PORTB = 0b10000000; Vdelay_ms(delay2);
      PORTB = 0b00000000; Vdelay_ms(delay2);
 // ********* End of LED Test ********
Loop:
switch (PORTA.F4) {

   case 0 : PORTA.F0=1;                // SW1 Indicator ON
 a = rand();
            while (a > 6) {
              a = a-6;

  };switch (a) {

            case (1) : PORTB = 0x00; Vdelay_ms(delay2); PORTB = 0b00001000 ;
                        Vdelay_ms(delay2); break;
            case (2) : PORTB = 0x00; Vdelay_ms(delay2); PORTB = 0b00010100 ;
                        Vdelay_ms(delay2); break;
            case (3) : PORTB = 0x00; Vdelay_ms(delay2); PORTB = 0b01001001 ;
                        Vdelay_ms(delay2); break;
            case (4) : PORTB = 0x00; Vdelay_ms(delay2); PORTB = 0b01100011 ;
                        Vdelay_ms(delay2); break;
            case (5) : PORTB = 0x00; Vdelay_ms(delay2); PORTB = 0b01101011 ;
                        Vdelay_ms(delay2); break;
            case (6) : PORTB = 0x00; Vdelay_ms(delay2); PORTB = 0b01110111 ;
                        Vdelay_ms(delay2); break;
                                       }; break;

   case 1 : PORTA.F0=0;                // SW1 Indicator ON
            Delay_ms(200);

                             }goto  Loop;
                             }
 
Although an idea came to me, to start the game with some continous attempts of switch, which get in the middle where it is not that much clear which number is next, but still that's a bug I wish to remove if possible. any idea?
 
srand works with rand, srand sets the seed that rand uses when numbers are generated, if you don't change srand you'll always get the same series of numbers from rand. One way to do this would be to store the counter of the number of random numbers generated when the circuit switches off, perhaps in eeprom or flash memory, and when the program starts back up to load this value into srand. Another option is leaving a timer running continously and when the button is pressed to generate a random number use that as the intial seed.

For more information on why this occurs read this Wikipedia article which describes the basics of how psuedo random number generators like this work. There are more sophisticated algryhms for generating random numbers out there but it is actually very difficult to get true randomness out of a computer system.
Linear feedback shift register - Wikipedia, the free encyclopedia
 
I had the exact same trouble when I designed a DICE program. The answer was to create a random number from the time delay of the player.

Here is the .asm
 

Attachments

  • Dice-1.asm
    6.5 KB · Views: 132
Thanks Sceadwian, I've read the article and made a more solid idea about the subject, thanks.


colin55, although I prefer the classical Dice Face with LED, it is nice to use 7-Segment, but what is the clever idea you used to make a FIVE output and ONE input to give SEVEN segments driver ?!. I tried to figure it out from your code but failed. Is there any sort of multiplexing ? or you did something in the hardware to solve this problem ?!

Your solution for random numbers is practical enough not to let the players to figure out. I'll try to implement it in my Dice code. Thanks again.
 
Why re-invent the wheel. Put sw on GP3 to ground with 47k pullup Program has "roll" and "slow down."

I used a PIC12F629 which has only 5 outputs and 1 input.
Here is the .asm for 7 LEDs. Call them a b c d e f g a b are on the top row c d e are the middle row fg are the bottom row
GP1 via a 68R goes to b f
GP2 via 68R goes to a g
GP4 via 82R goes d
GP5 via 68R goes to c e
 

Attachments

  • LED-Die-H.asm
    5.3 KB · Views: 109
Last edited:
ahah, for each paly, you are switching the whole circuit OFF and then ON ?, that's another idea to implement and save one I/O or just I.
Thanks for all.
 
hi, I am a newbie to PIC chip programming... my next venture would be to use a buzzer connected to a 16f677 chip, to play ringtones of any sort.. mostly using changing frequencies and time periods. Can someone please give me a link to any tutorial which will let me get the required data............ i have already gone through the basics of programing. Am programming in machine language..

Thanx
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top