Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

RedRabbit

noob wants advanced programmer to check code

This topic is 5226 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hey guys Id just like to know if my code is OK. please make corrections/changes to any of it or just some suggestions. thanks.
//Random number generator by RedRabbit

#include <iostream>
#include <ctime>

using namespace std;

int getRandInt(int begin, int end);

int main()
{
	int x;
	int y;

	cout << "Random integer number generator.\nBy RedRabbit.\nEnjoy!\n\n\n\n" << endl;
	cout << endl;
	cout << "Begin with (integer): ";
	cin >> x;
	cout << "End with (integer): ";
	cin >> y;
	cout << getRandInt(x,y) << endl; 
	return 1;
}

int getRandInt(int begin, int end)
{
	srand(time(NULL));
	return (rand()%end+begin);
}
Im taking C++ one step at a time so dont flame because this is a noob program please I know it is. I know how to program polygons in OpenGL im just refreshing my knowledge of the basics.

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by RedRabbit
hey guys Id just like to know if my code is OK. please make corrections/changes to any of it or just some suggestions. thanks.


//Random number generator by RedRabbit

#include <iostream>
#include <ctime>

using namespace std;

int getRandInt(int begin, int end);

int main()
{
int x;
int y;

cout << "Random integer number generator.\nBy RedRabbit.\nEnjoy!\n\n\n\n" << endl;
cout << endl;
cout << "Begin with (integer): ";
cin >> x;
cout << "End with (integer): ";
cin >> y;
cout << getRandInt(x,y) << endl;
return 1;
}

int getRandInt(int begin, int end)
{
srand(time(NULL));
return (rand()%end+begin);
}


Im taking C++ one step at a time so dont flame because this is a noob program please I know it is. I know how to program polygons in OpenGL im just refreshing my knowledge of the basics.


One, why have to endl;''s, especially when you already got 4 \n''s before that. It''s pretty screwy. Also, I''d suggest having main returning 0, not 1.

Share this post


Link to post
Share on other sites
call srand() only once in the beginning of program. If you intend to call getRandInt() many times, it would screw up the random numbers.

Share this post


Link to post
Share on other sites
I see 2 things that could be wrong.

1. Generally you only call srand once per program, Instead of once per rand call. There are several reasons for this the most blatant being if you where to call getRandInt twice in quick succession, the srand would be the same (since time couldn''t update that fast) and you would receive the same number twice in a row.

2. I assume getRandInt returns an int between begin and end (inclusive). So rand()%end+begin is wrong, you want rand()%(end-begin+1)+begin.

Although it''s fine here (simple program) you want to make varibles close to where you use, so you know when and where they''re used and to also reduce uneeded constructors.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!