![]() |
|
#1
|
|||
|
|||
Function that returns a random numberI need to write a function called generate that returns a random number between x and y. I have writen the code below, but it will not compile. Any suggestions would be appriciated.
Last edited by Tori : 02-Nov-2004 at 16:25.
Reason: Need to include code
|
|||
|
#2
|
|||
|
|||
|
Quote:
Look at my response to your other question for hints on getting it to compile. If you have additional questions, post the specific questions with your current code. Regards, Dave |
|
#3
|
||||
|
||||
|
Quote:
__________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#4
|
||||
|
||||
|
Quote:
See Dave's answer for solving this problem. I just want to point out a couple of things in your algorithm. First of all, what I understand from your code, you want to generate a random number between x and y. However, this line of code will generate a number between x and x + y : CPP / C++ / C Code:
I suggest you use this instead: CPP / C++ / C Code:
Also, if you will run the program several times, you will notice that if you provide the same range values, it will give you the same result! This might not bother you, but in case it does, you could use srand to initialise the random number generator. In order to really make it random, you could use the current time (it will be different next time you run the program!). Be carefull: ony use srand one time in your program, before the call to rand; this is my version of your program CPP / C++ / C Code:
Kind regards, Luci __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#5
|
||||
|
||||
|
Quote:
Parentheses will help understanding of this line just a little better: CPP / C++ / C Code:
__________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
Recent GIDBlog
Accepted for Ph.D. program by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Error: (function) undeclared -first use of this function | crystalattice | C++ Forum | 6 | 01-Nov-2004 05:36 |
| Anyone can write a program code for this??? | chriskan76 | C Programming Language | 1 | 19-Oct-2004 21:25 |
| Revising Script style ?????? | pepee | MySQL / PHP Forum | 4 | 14-Apr-2004 05:59 |
| urgent needs :apache + random function | jack | Apache Web Server Forum | 0 | 19-Jan-2004 18:41 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The