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 02-Nov-2004, 16:24
Tori Tori is offline
New Member
 
Join Date: Oct 2004
Posts: 23
Tori is on a distinguished road

Function that returns a random number


I 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.
Attached Files
File Type: txt Homework_4-2.txt (922 Bytes, 32 views)
Last edited by Tori : 02-Nov-2004 at 16:25. Reason: Need to include code
  #2  
Old 02-Nov-2004, 18:14
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Tori
I 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.

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  
Old 02-Nov-2004, 21:56
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by davekw7x
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
Also, please post short segments of code in your post, not as an attachment. More people will look at the code this way. I for one don't download attachments -- takes too long, can be dangerous, etc. And be sure to use code tags. "Don't post code without them" as Karl Malden might have said (how many of you remember where that comes from? )
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #4  
Old 03-Nov-2004, 01:27
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by Tori
I 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.

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:
	return ( x + rand() % (y + 1));

I suggest you use this instead:

CPP / C++ / C Code:
	return ( x + rand() % (y + 1 - x));

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:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "conio.h"


using std::cout;
using std::cin;
using std::endl;

int generate (int, int);

int main()
{
	int x;	// smallest number
	int y;	// largest number
	int z;	// random number

	cout << "Enter smallest number in range\n";
	cin >> x;

	cout << "Enter largest number in range\n";
	cin >> y;

srand(static_cast<unsigned>(time(0)));

	z = generate(x,y);
	cout << "The random number is:" << z << endl;
		
	getch();

   return 0;    // indicate the program ended successfully
}

int generate (int x, int y)
{

return ( x + rand() % (y + 1 - x));

//	return ( x + rand() % (y + 1));

}

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  
Old 03-Nov-2004, 21:48
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by LuciWiz
I suggest you use this instead:
CPP / C++ / C Code:
return ( x + rand() % (y + 1 - x));
Nitpick time....

Parentheses will help understanding of this line just a little better:
CPP / C++ / C Code:
return ( x + (rand() % (y + 1 - x)));
Yes, we know the % is done before a +, but not everyone does. And for those that don't quite have a grasp of the operator hierarchy, this will help. Even I have to think about it sometimes...
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
 
 

Recent GIDBlogAccepted for Ph.D. program 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
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

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


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