Random Number generator

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
NeonNinja♦
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 38
Joined: Sat Oct 25, 2008 4:55 pm
Location: Under your bed eating your couch :D

Random Number generator

Post by NeonNinja♦ »

I was following a tutorial to do this, and whenever i guess it says too high! and too low! at the same time any ideas?

Code: Select all

#include <iostream>
#include <ctime>
using namespace std;

int main(void)
{
	int iGumballs;
	int iUserguess;
	int iGuesses = 0;

	while(true)
	{
		system("CLS");
		cin.clear();
		iGuesses = 0;

	srand(static_cast<unsigned int>(time(0)));
	iGumballs = rand()%1000+1;
	cout << "How many gumballs are in the jar!"
		<<endl;
	do
	{
	cout << "Enter your guess: ";
	cin >> iUserguess;
	if(iUserguess > iGumballs);
	{
		cout << "Too High!" << endl << endl;
	}
    if(iUserguess < iGumballs)
	{
		cout << "Too Low!" << endl << endl;
	}
	iGuesses ++;
	}while(iUserguess > iGumballs || iUserguess < iGumballs);
	cout << "You guessed Correct! Cookie for you!" << endl << endl;
	cout << "You took " << iGuesses << " guesses" << endl << endl;
	system("PAUSE");
	}
	return 0;
}
So what did i do wrong? :P

Thanks for your time and responses
-Neon Ninja♦
"Java is C++ without the guns, knives, and clubs" <------- So true.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Random Number generator

Post by Falco Girgis »

Code: Select all

   if(iUserguess > iGumballs);
   {
      cout << "Too High!" << endl << endl;
   }
Since you put the semicolon after your if statement, the block after it is executed regardless of the condition.

Remove that semicolon.
User avatar
NeonNinja♦
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 38
Joined: Sat Oct 25, 2008 4:55 pm
Location: Under your bed eating your couch :D

Re: Random Number generator

Post by NeonNinja♦ »

Ahh ok thanks :D
"Java is C++ without the guns, knives, and clubs" <------- So true.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Random Number generator

Post by MarauderIIC »

The reason for that is because this is valid:

Code: Select all

if (condition) doSomething;
which is equivalent to:

Code: Select all

if (condition) {
    doSomething;
}
And also, this is valid:

Code: Select all

stuff
stuff
stuff
{
    faux block
    some more statements
    variables here go out of scope after the closing brace
}
stuff2
stuff2
And so this:

Code: Select all

if (doSomething);
{
    faux block, do stuff
    doStuff
}
is the same as this:

Code: Select all

if (doSomething)
    doNothing;
{
    faux block
}
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
NeonNinja♦
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 38
Joined: Sat Oct 25, 2008 4:55 pm
Location: Under your bed eating your couch :D

Re: Random Number generator

Post by NeonNinja♦ »

Lol my brain just exploded :lol: im not that good but thanks :D

EDIT: Oh and i made this for a halloween thread but i cant find the errors can you help me with this i keep getting an unassigned '\'

Code: Select all

#include <iostream>
#include <ctime>
using namespace std;

int main(void)
{
 system("TITLE Halloween Program Art");
	 system("COLOR 123");

cout << "               .                     " << endl;                              
 cout << "              //                     " << endl;        
  cout << "    _.-"""""'//-'""""               << endl;
 cout << " .', ,  , , : : ` ` `  `. \           " << endl;
 cout << "  / , , \'-._ : :_.-'/ ` ` \         " << endl;                              
 cout << " / , ,  :\(_)\  /(_)/ : ` ` \        " << endl;
 cout << "| , ,  ,  \__//\\__/ . . ` ` |       " << endl;                                
 cout << "| . .:_  : : '--`: : . _: ; :|       " << endl;                                
 cout << "| : : \\_  _' : _: :__// , , |       " << endl;                                
 cout << " \ ` ` \ \/ \/\/ \_/  / , , /         " << endl;                              
 cout << " \ ` ` \_/\_/\_/\_/\/ , , /           " << endl;                            
 cout << "  `._ ` . :  :  :  , , _.'        " << endl;
 cout << "     `-..............-'              " << endl;
 cout << " ===================HAPPY=HALLOWEEN!======================" << endl;
 system("PAUSE");
 return 0;
}
"Java is C++ without the guns, knives, and clubs" <------- So true.
User avatar
NeonNinja♦
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 38
Joined: Sat Oct 25, 2008 4:55 pm
Location: Under your bed eating your couch :D

Re: Random Number generator

Post by NeonNinja♦ »

:lol: I wasn't supposed to show you that! Shhhhhh i was going to see if anyone could make something like that :D
"Java is C++ without the guns, knives, and clubs" <------- So true.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Random Number generator

Post by MarauderIIC »

Yeah, \ is an 'escape character'. Use \\ whenever you use \ if you want to write \.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
NeonNinja♦
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 38
Joined: Sat Oct 25, 2008 4:55 pm
Location: Under your bed eating your couch :D

Re: Random Number generator

Post by NeonNinja♦ »

uh oh still getting errors :cry:

------ Build started: Project: Gumball Project, Configuration: Debug Win32 ------
Compiling...
GumballGame.cpp
c:\users\collin\documents\visual studio 2008\projects\as\as\gumballgame.cpp(12) : error C2143: syntax error : missing ';' before 'constant'
c:\users\collin\documents\visual studio 2008\projects\as\as\gumballgame.cpp(12) : error C2143: syntax error : missing ';' before 'string'
c:\users\collin\documents\visual studio 2008\projects\as\as\gumballgame.cpp(12) : error C2563: mismatch in formal parameter list
c:\users\collin\documents\visual studio 2008\projects\as\as\gumballgame.cpp(12) : error C2568: '<<' : unable to resolve function overload
c:\program files\microsoft visual studio 9.0\vc\include\ostream(974): could be 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
with
[
_Elem=wchar_t,
_Traits=std::char_traits<wchar_t>
]
c:\program files\microsoft visual studio 9.0\vc\include\ostream(966): or 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
with
[
_Elem=char,
_Traits=std::char_traits<char>
]
c:\program files\microsoft visual studio 9.0\vc\include\ostream(940): or 'std::basic_ostream<_Elem,_Traits> &std::endl(std::basic_ostream<_Elem,_Traits> &)'
c:\users\collin\documents\visual studio 2008\projects\as\as\gumballgame.cpp(13) : warning C4129: ' ' : unrecognized character escape sequence
c:\users\collin\documents\visual studio 2008\projects\as\as\gumballgame.cpp(14) : warning C4129: ' ' : unrecognized character escape sequence
c:\users\collin\documents\visual studio 2008\projects\as\as\gumballgame.cpp(15) : warning C4129: '(' : unrecognized character escape sequence
c:\users\collin\documents\visual studio 2008\projects\as\as\gumballgame.cpp(15) : warning C4129: ' ' : unrecognized character escape sequence
c:\users\collin\documents\visual studio 2008\projects\as\as\gumballgame.cpp(15) : warning C4129: ' ' : unrecognized character escape sequence
c:\users\collin\documents\visual studio 2008\projects\as\as\gumballgame.cpp(16) : warning C4129: '_' : unrecognized character escape sequence
c:\users\collin\documents\visual studio 2008\projects\as\as\gumballgame.cpp(19) : warning C4129: ' ' : unrecognized character escape sequence
c:\users\collin\documents\visual studio 2008\projects\as\as\gumballgame.cpp(19) : warning C4129: ' ' : unrecognized character escape sequence
c:\users\collin\documents\visual studio 2008\projects\as\as\gumballgame.cpp(19) : warning C4129: '/' : unrecognized character escape sequence
c:\users\collin\documents\visual studio 2008\projects\as\as\gumballgame.cpp(19) : warning C4129: '/' : unrecognized character escape sequence
c:\users\collin\documents\visual studio 2008\projects\as\as\gumballgame.cpp(19) : warning C4129: '/' : unrecognized character escape sequence
c:\users\collin\documents\visual studio 2008\projects\as\as\gumballgame.cpp(19) : warning C4129: '_' : unrecognized character escape sequence
c:\users\collin\documents\visual studio 2008\projects\as\as\gumballgame.cpp(20) : warning C4129: ' ' : unrecognized character escape sequence
c:\users\collin\documents\visual studio 2008\projects\as\as\gumballgame.cpp(20) : warning C4129: '_' : unrecognized character escape sequence
c:\users\collin\documents\visual studio 2008\projects\as\as\gumballgame.cpp(20) : warning C4129: '_' : unrecognized character escape sequence
c:\users\collin\documents\visual studio 2008\projects\as\as\gumballgame.cpp(20) : warning C4129: '_' : unrecognized character escape sequence
c:\users\collin\documents\visual studio 2008\projects\as\as\gumballgame.cpp(20) : warning C4129: '_' : unrecognized character escape sequence
c:\users\collin\documents\visual studio 2008\projects\as\as\gumballgame.cpp(20) : warning C4129: '/' : unrecognized character escape sequence
Build log was saved at "file://c:\Users\Collin\Documents\Visual Studio 2008\Projects\as\as\Debug\BuildLog.htm"
Gumball Project - 4 error(s), 18 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
"Java is C++ without the guns, knives, and clubs" <------- So true.
User avatar
NeonNinja♦
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 38
Joined: Sat Oct 25, 2008 4:55 pm
Location: Under your bed eating your couch :D

Re: Random Number generator

Post by NeonNinja♦ »

I know its possible but i dont think i can do it T_T
"Java is C++ without the guns, knives, and clubs" <------- So true.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Random Number generator

Post by Falco Girgis »

Code: Select all

  cout << "    _.-"""""'//-'""""               << endl;
You have to use the escape characters before " also. So change " to \"
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Random Number generator

Post by MarauderIIC »

Anywhere that you want " to appear, anyway.
So it should look like this

Code: Select all

cout << "    _.-\"\"\"\"\"'//-'\"\"\""               << endl;
Although you probably meant

Code: Select all

cout << "    _.-\"\"\"\"\"'//-'\"\"\"\"              " << endl;
(extra escaped ", another " after a bunch of spaces so it's easier to recognize)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
NeonNinja♦
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 38
Joined: Sat Oct 25, 2008 4:55 pm
Location: Under your bed eating your couch :D

Re: Random Number generator

Post by NeonNinja♦ »

:mrgreen: Mostly I think i wasn't experienced enough to know this yet, so i am going to try to make a random number generator where it tests out any possible 10 digit codes or below 10 digits :D (im not a hacker lolz)

EDIT: Don't know how i am going to make a code breaker at my level...... just going to have to research the rand function and modulus

But yeah, when i get better im gonna get back on this project :D but it would have been fun, knowing Halloween in is like 5/6 days :lol:

Thanks again for all your help and i really appreciate it :D
"Java is C++ without the guns, knives, and clubs" <------- So true.
User avatar
sparda
Chaos Rift Junior
Chaos Rift Junior
Posts: 291
Joined: Tue Sep 23, 2008 3:54 pm

Re: Random Number generator

Post by sparda »

EDIT: Sorry, just fixed the congruence formula... I did a stupid mistake before.

You know, since rand() is still systemic (thus not truly random), why not generate your own pseudorandom numbers from scratch using linear congruential methods instead of using the rand() function? (thats the way you really learn!).
Here is some information, although you should really read a book on discrete math: http://en.wikipedia.org/wiki/Linear_con ... _generator

You would need a multiplier a, a modulus m, increment c, and seed xN (N is a subscript in this case, for N is an element of the Natural numbers). The universe of discourse being, for a, c, and xN:
2 <= a < m,
0 <= c < m,
0 <= xN < m.

For instance, you can define a new function yourself and use it just like rand(), say myrand():

Code: Select all

int myrand(int seed)
{
   int a = 8;      // You can change this
   int c = 4;      // You can change this
   int m = 10;   // This would give you random numbers between 0 and 10;
   int  xN = seed;
   return (xN*a + c) % m;
}
Of course, the important thing is to seed it accordingly between function calls. You can use the traditional method of seeding in terms of your PC's internal clock, or use other clever solutions. This kinda interesting I think, no? (Hint: use the time.h header )
Last edited by sparda on Tue Oct 28, 2008 7:48 pm, edited 1 time in total.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Random Number generator

Post by MarauderIIC »

Hint: To prevent random numbers from repeating, try this! Especially useful if you're generating a lot of them fast!
I've always wondered if random #s were unique for every seed, never did find out.

Code: Select all

unsigned int lastRand;
unsigned int result;
srand(time(NULL)); //primer
while (1) {
    lastRand = rand();
    result = lastRand % mod;
    cout << result << endl;
    srand(lastRand);
}
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
NeonNinja♦
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 38
Joined: Sat Oct 25, 2008 4:55 pm
Location: Under your bed eating your couch :D

Re: Random Number generator

Post by NeonNinja♦ »

Hey im sorry didn't have any time to post with the first day of school and all..

But i decided what my first project was going to be, hopefully i can make a random code generator used for GOOD. (lol)
I would use it to help recover password to the victims, such as my brother who lost his Steam account (Steam is a gaming thing lol look it up if u want to add me my steam is collin311) but its going to be hard.. but thats how you learn :D Oh and thanks for the hint marauder (sorry for misspell:D)
"Java is C++ without the guns, knives, and clubs" <------- So true.
Post Reply