GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Oct-2005, 01:21
Jonnyz007 Jonnyz007 is offline
New Member
 
Join Date: Oct 2005
Posts: 4
Jonnyz007 is on a distinguished road
Exclamation

random number generation issues


hi all, ive just started a micro controllers section at uni, and my task is to program an AVR micro controller to perform an electronic dice function. ive sorted out all inputs and outputs, and what to do with a result 1, 2, etc. just struggling with the random number generation!

here is what i have for the code thus far:

CPP / C++ / C Code:

#include <stdio.h>
#include <90s2313.h>
#include <stdlib.h>  
 

void main(void)
{  
int random;
random = rand();

DDRB = 0xFB;
DDRD = 0xFF;
PORTB = 0x00;  

while(1)
{  
random = random / 5461.1667; 

if (random <=1);
random = 1;

else if (random >1 & random <= 2);  
random = 2;

else if (random >2 & random <= 3);  
random = 3;   

else if (random >3 & random <= 4);  
random = 4;

else if (random >4 & random <= 5);  
random = 5;       

else (random >5 & random <= 6);  
random = 6;


switch (random)
 {
 case 1:
 { PORTD = 0x08;
 break;
 }
 case 2:
 { PORTD = 0x14;
 break;
 }
 case 3:
 { PORTD = 0x1C;
 PORTB = 0x00;
 break;
 }
 case 4:
 { PORTD = 0x54;
 PORTB = 0x01;
 break;
 }
 case 5:
 { PORTD = 0x5C;
 PORTB = 0x01;
 break;
 }
 case 6:
 { PORTD = 0x76;
 PORTB = 0x01;
 break;
 }
}
} 
}


as you can see, ive generated a random number, but can't seem to process it? - rand = number between 0 -32767

32767 / 5461.667 = 5.999 etc - so using < / > to round up / down. any faster ways?

thanks
jonny
  #2  
Old 27-Oct-2005, 06:35
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough
Smile

Re: random number generation issues


Hi Jonnyz,

Welcome to the GID Forums.
Thank you for reading the guidelines.

The main error is that we have a semicolon ; after every if or else if statement. The semicolon indicates the end of a statement and we should not put that after a if statement. Here it is:
CPP / C++ / C Code:

if (random <=1)
random = 1;

else if (random >1 && random <= 2)  
random = 2;

else if (random >2 && random <= 3)  
random = 3;   

else if (random >3 && random <= 4)  
random = 4;

else if (random >4 && random <= 5)  
random = 5; 


Next, we should not check for any values in the else statement.
Only if all the conditions of if and else if are false, else is executed.
So we should remove the condition after the else statement. Here it is:
CPP / C++ / C Code:
else 
random = 6;
Next, we are checking two conditions inside the if statements.
So we should use && instead of a single &;
The single & is bitwise operator whereas && is a logical operator.
So if we want to combine two conditions, we should use the && operator.

Now going on to the random number generation.
First, we should put the random = rand(); inside the while loop because we want to generate a random number for every loop.
That would solve all the problems.

CPP / C++ / C Code:
while(1)
{  
    random = rand();
    random = random / 5461.1667; 
    //code continues......

But we can still improve the program by giving the random number generation a seed value to start with.
If we dont use a seed, the random number generation will be identical every time we run the program.
i.e if we get the output of the first run as:
3 1 5 3 2 5 4 ...
then if we run the program a second time we will get the same output
3 1 5 3 2 5 4 ...
So every time we run the program we will get the same output.

To solve this, we can give a seed value.
The seed is used to kick off the sequence of random numbers. It is explicitly specified using the srand function. srand takes an unsigned int as an argument and sets the seed used for generating random numbers. Usually, the system time is a good choice as an argument for srand. It will be different each time the program is run. Thus, results will also be different.

Here is how we should use the srand function:
CPP / C++ / C Code:
srand( ( unsigned ) time( NULL ) );

We can check how the random number is generated using a printf statement inside the while loop. This will give us an idea of the random numbers generated and cross check the output.

Summarising, here is the code:
CPP / C++ / C Code:
srand( ( unsigned ) time( NULL ) );
while(1)
{  
    random = rand();
    random = random / 5461.1667; 


    if (random <=1)
        random = 1;

    else if (random >1 && random <= 2)  
        random = 2;

    else if (random >2 && random <= 3)  
        random = 3;   

    else if (random >3 && random <= 4)  
        random = 4;

    else if (random >4 && random <= 5)  
        random = 5;       

    else 
        random = 6;

printf ( "Random number generated is %d \n" , random );


Cheers,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #3  
Old 27-Oct-2005, 12:33
QED's Avatar
QED QED is offline
Member
 
Join Date: Feb 2005
Location: Hudson Valley, NY
Posts: 231
QED is a jewel in the roughQED is a jewel in the roughQED is a jewel in the rough

Re: random number generation issues


Good suggestions from Paramesh. However, I recommend that you include some way of specifying or controlling the seed value for your RNG. Otherwise, you have no way of repeating your results. When testing, you must be able to control the initial conditions so your results are repeatable.

Thus, either use a command line option/argument for the random seed (if that is appropriate for your program), or use preprocessor directives with a DEBUG macro to force the use of a specific seed for testing purposes. For example,
CPP / C++ / C Code:
#ifdef DEBUG
  srand(0);
#else
  srand(time(NULL));
#endif
For the debug section, any positive int value will work fine, too. Just remember to define the symbol DEBUG if you want to build your test version.

-Matthew-
  #4  
Old 27-Oct-2005, 17:04
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: random number generation issues


Hi Mathhew,

Thank you for your idea.
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #5  
Old 27-Oct-2005, 18:33
Jonnyz007 Jonnyz007 is offline
New Member
 
Join Date: Oct 2005
Posts: 4
Jonnyz007 is on a distinguished road
Unhappy

Re: random number generation issues


hey guys, thanks heaps for your help!

couple more issues (sorry i really am a novice!)

im using codevisionAVR to program my micro controller, and does not have a <time.h> function thing to include - therefore

srand( ( unsigned ) time( NULL ) );

only returns - undefined symbol 'time'

anyway around this? can i download time.h from somewhere?

also the program needs to generate a new random number, and display it everytime a push button is pressed. - therefore a looping program. how do i do this?

my code as now:

CPP / C++ / C Code:
#include <stdio.h>
#include <90s2313.h>
#include <stdlib.h>  
#include <math.h>
#include <time.h>  // error there is no time.h file!!

void main(void)
{  
int random;   


DDRB = 0xFB;
DDRD = 0xFF;
PORTB = 0x00;  


srand( ( unsigned ) time( NULL ) );
while(1)
{  
    random = rand();
    random = random / 5461.1667; 


    if (random <=1)
        random = 1;

    else if (random >1 && random <= 2)  
        random = 2;

    else if (random >2 && random <= 3)  
        random = 3;   

    else if (random >3 && random <= 4)  
        random = 4;

    else if (random >4 && random <= 5)  
        random = 5;       

    else 
        random = 6;


switch (random)
 {
 case 1:
 { PORTD = 0x08;
 break;
 }
 case 2:
 { PORTD = 0x14;
 break;
 }
 case 3:
 { PORTD = 0x1C;
 PORTB = 0x00;
 break;
 }
 case 4:
 { PORTD = 0x54;
 PORTB = 0x01;
 break;
 }
 case 5:
 { PORTD = 0x5C;
 PORTB = 0x01;
 break;
 }
 case 6:
 { PORTD = 0x76;
 PORTB = 0x01;
 break;   
 }
}
} 
}

thanks again! big ups
  #6  
Old 27-Oct-2005, 18:53
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: random number generation issues


Hi Jonnyz,

Which compiler do you use?
Which OS do you use?


Regarding the random number generation with every push button,
Just add a getchar() function inside a while loop. Inside the while loop, generate the random numbers.
try this sample code:
CPP / C++ / C Code:

srand( ( unsigned ) time( NULL ) );
while(1)
{  
    random = rand();
    random = random / 5461.1667; 


    if (random <=1)
        random = 1;

    else if (random >1 && random <= 2)  
        random = 2;

    else if (random >2 && random <= 3)  
        random = 3;   

    else if (random >3 && random <= 4)  
        random = 4;

    else if (random >4 && random <= 5)  
        random = 5;       

    else 
        random = 6;

    printf("The random number is %d \n", random );

    getchar();          //wait for user input.
}

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #7  
Old 27-Oct-2005, 19:04
Jonnyz007 Jonnyz007 is offline
New Member
 
Join Date: Oct 2005
Posts: 4
Jonnyz007 is on a distinguished road

Re: random number generation issues


hey thanks again for your reply:

the codevision is a C Compiler - on a windows xp computer.

the code needs to work more like the following, elementary example:

1. if (button is pushed)

2. then generate new random number

3. display number

4. go back to beginning and wait for button to be pushed again.

ive got some more code im trying now: still wont work perfectly

CPP / C++ / C Code:
#include <stdio.h>
#include <90s2313.h>
#include <stdlib.h>  
#include <math.h>

void second(void)
{    
int random;

DDRB = 0xFB;
DDRD = 0xFF;
PORTB = 0x00;  

        random = rand();                          //generates random number
        random = random / 5461.1667; 

    if (random <=1)                                 //converts to 1-6
        random = 1;

    else if (random >1 && random <= 2)  
        random = 2;

    else if (random >2 && random <= 3)  
        random = 3;   

    else if (random >3 && random <= 4)  
        random = 4;

    else if (random >4 && random <= 5)  
        random = 5;       

    else 
        random = 6;

switch (random)                 //turns on display LED's
 {
 case 1:                            //eg. LED pattern shows a 1
 { PORTD = 0x08;
 break;
 }
 case 2:
 { PORTD = 0x14;
 break;
 }
 case 3:
 { PORTD = 0x1C;
 PORTB = 0x00;
 break;
 }
 case 4:
 { PORTD = 0x54;
 PORTB = 0x01;
 break;
 }
 case 5:
 { PORTD = 0x5C;
 PORTB = 0x01;
 break;
 }
 case 6:
 { PORTD = 0x76;
 PORTB = 0x01;
 break;   
 } 
 }
}
       
       
       

void main(void)
{   
while(1)
if (PINB.2 ==1)               //if button is pushed 
 
second();                      //then run random number and display program
}

again, all help is GREATLY appreciated, anyone need help with PLC's ask me, thats my background :-P
  #8  
Old 27-Oct-2005, 19:11
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: random number generation issues


Hi Jonnyz,

You can try download Dev-C++ with mingw compiler.
You can download the latest version here.
Note that dev-c++ is not a compiler. It is an IDE with mingw compiler(Minimalist GNU for windows compiler).

There are also many good compilers.
I also like the cygwin very much. Microsoft has Free visual c++ toolkit.
Borland has also a free command line compiler.

You can also see what others use in this thread:
Which compiler do you use?

Quote:
Originally Posted by Jonnyz
the code needs to work more like the following, elementary example:

1. if (button is pushed)

2. then generate new random number

3. display number

4. go back to beginning and wait for button to be pushed again.

Did you try my sample code?

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #9  
Old 27-Oct-2005, 19:13
Jonnyz007 Jonnyz007 is offline
New Member
 
Join Date: Oct 2005
Posts: 4
Jonnyz007 is on a distinguished road

Re: random number generation issues


hey, found my way did the trick, thanks very VERY much for your assitance though! cheers, jonny
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Help with my Cards Program (C++) krisopotamus C++ Forum 2 06-Oct-2005 16:48
Knights Tour - Reloaded . kobi_hikri C Programming Language 12 03-Oct-2005 12:15
Function that returns a random number Tori C++ Forum 4 03-Nov-2004 20:48
Anyone can write a program code for this??? chriskan76 C Programming Language 1 19-Oct-2004 20:25
Random() : Make each number onlu appear once NiXeN C++ Forum 3 13-Jan-2004 04:47

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:22.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.