![]() |
|
#1
|
|||
|
|||
random number generation issueshi 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:
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
|
||||
|
||||
Re: random number generation issuesHi 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:
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:
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:
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:
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:
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
|
||||
|
||||
Re: random number generation issuesGood 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:
-Matthew- |
|
#4
|
||||
|
||||
Re: random number generation issuesHi 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
|
|||
|
|||
Re: random number generation issueshey 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:
thanks again! big ups |
|
#6
|
||||
|
||||
Re: random number generation issuesHi 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:
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
|
|||
|
|||
Re: random number generation issueshey 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:
again, all help is GREATLY appreciated, anyone need help with PLC's ask me, thats my background :-P |
|
#8
|
||||
|
||||
Re: random number generation issuesHi 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:
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
|
|||
|
|||
Re: random number generation issueshey, found my way did the trick, thanks very VERY much for your assitance though! cheers, jonny
|
Recent GIDBlog
Once again, no time for hobbies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Need Help with my Cards Program (C++) | krisopotamus | C++ Forum | 2 | 06-Oct-2005 17:48 |
| Knights Tour - Reloaded . | kobi_hikri | C Programming Language | 12 | 03-Oct-2005 13:15 |
| Function that returns a random number | Tori | C++ Forum | 4 | 03-Nov-2004 21:48 |
| Anyone can write a program code for this??? | chriskan76 | C Programming Language | 1 | 19-Oct-2004 21:25 |
| Random() : Make each number onlu appear once | NiXeN | C++ Forum | 3 | 13-Jan-2004 05:47 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The