Page 1 of 2

Random Number generator

Posted: Sun Oct 26, 2008 2:08 pm
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♦

Re: Random Number generator

Posted: Sun Oct 26, 2008 2:22 pm
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.

Re: Random Number generator

Posted: Sun Oct 26, 2008 2:31 pm
by NeonNinja♦
Ahh ok thanks :D

Re: Random Number generator

Posted: Sun Oct 26, 2008 8:13 pm
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
}

Re: Random Number generator

Posted: Sun Oct 26, 2008 8:17 pm
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;
}

Re: Random Number generator

Posted: Sun Oct 26, 2008 8:21 pm
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

Re: Random Number generator

Posted: Sun Oct 26, 2008 8:28 pm
by MarauderIIC
Yeah, \ is an 'escape character'. Use \\ whenever you use \ if you want to write \.

Re: Random Number generator

Posted: Sun Oct 26, 2008 8:29 pm
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 ==========

Re: Random Number generator

Posted: Sun Oct 26, 2008 8:34 pm
by NeonNinja♦
I know its possible but i dont think i can do it T_T

Re: Random Number generator

Posted: Sun Oct 26, 2008 8:40 pm
by Falco Girgis

Code: Select all

  cout << "    _.-"""""'//-'""""               << endl;
You have to use the escape characters before " also. So change " to \"

Re: Random Number generator

Posted: Sun Oct 26, 2008 8:49 pm
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)

Re: Random Number generator

Posted: Sun Oct 26, 2008 8:54 pm
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

Re: Random Number generator

Posted: Tue Oct 28, 2008 12:45 pm
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 )

Re: Random Number generator

Posted: Tue Oct 28, 2008 1:30 pm
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);
}

Re: Random Number generator

Posted: Tue Oct 28, 2008 7:09 pm
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)