h3%5kr3w Posted December 8, 2009 Share Posted December 8, 2009 ok, so I'll give some back story. I am making this program to play a good joke (nothing else, and this guy is totally cool, I'm not going to get into any trouble with it) by using a switchblade to put this program into his desktop and a few old servers that are never really used (at all). I am using Bloodshed's C++ to code it cause it's just way simpler to get started with than Visual Studio (I have this as well though) All the program should do is make 3 random numbers, and allot those to how long the program waits, how many hz the beep is and the duration of said beep. Then it just does it over again. It does not hide itself *yet* I will be doing that in the finishing stages. Think of it as a software version of the annoy-a-tron. This code does work, it's just that instead of the numbers being completely random each time, it's stepping the number up @ random. Any help is much appreciated (specially since I want to get him BEFORE exams are over [next monday]) #include <iostream> #include <windows.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <cstdlib> #include <ctime> using namespace std; int randomNumber(); int randomNumber2(); int randomNumber3(); int main() { dehehe: //basis for goto loop DWORD number = 0; //convert to Dword for use with Beep (milliseconds of beep duration) DWORD number2 = 0; //convert to Dword for use with Beep (hz of beep) DWORD number3 = 0; //convert to Dword for use with Beep (time to wait until the loop) number = randomNumber(); //calls random number generator number2 = randomNumber2(); //calls random number generator number3 = randomNumber3(); //calls random number generator cout << number << ":"<< number2 << ":"<< number3 << ":"; //used to debug randomizers //Beep(number2,number); // x hertz (C5) for x milliseconds *Uncomment to hear beepys!* Sleep(number3); //sleep timer goto dehehe; //loop cin.get(); // wait.. if it ever get's here.. which it wont (well.. shouldn't) return 0; } int randomNumber() { int randNumber = 0; srand(time(0)); //This seeds the random number generator randNumber = rand(); //This gets a random number as an integer const int MAX = 800; //Sets a constant variable for the max int number = (randNumber % MAX) + 1; return number; //sends random number back to main() } int randomNumber2() { srand(time(0)); //This seeds the random number generator int randNumber2 = rand(); //This gets a random number as an integer const int MAX = 2000; //Sets a constant variable for the max int number2 = (randNumber2 % MAX) + 1; return number2; //sends random number back to main() } int randomNumber3() { srand(time(0)); //This seeds the random number generator int randNumber3 = rand(); //This gets a random number as an integer const int MAX = 10000; //Sets a constant variable for the max int number3 = (randNumber3 % MAX) + 1; return number3; //sends random number back to main() } Quote Link to comment Share on other sites More sharing options...
Kerberos Posted December 8, 2009 Share Posted December 8, 2009 Your problem is your srand(). time(0) is accurate only to the second and as such is returning the same value for each call. This means that you're passing srand() the same number each time you call it (because the three functions are executed in far less than a second). Move srand(time(0)); up to the main function so that it only runs once and you'll be fine. I made a few simple optimizations that you might consider. I know it's not a big project or anything, but it's always nice to have short, simple code that gets things done just right :). Here's my version if you're interested: #include <iostream> #include <windows.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <cstdlib> #include <ctime> using namespace std; int main() { srand(time(0)); dehehe: //basis for goto loop DWORD number = 0; //convert to Dword for use with Beep (milliseconds of beep duration) DWORD number2 = 0; //convert to Dword for use with Beep (hz of beep) DWORD number3 = 0; //convert to Dword for use with Beep (time to wait until the loop) number = rand() % 800; //calls random number generator number2 = rand() % 2000; //calls random number generator number3 = rand() % 10000; //calls random number generator //cout << number << " : " << number2 << " : " << number3 << ": "; //used to debug randomizers Beep(number2,number); // x hertz (C5) for x milliseconds *Uncomment to hear beepys!* Sleep(number3); //sleep timer goto dehehe; //loop cin.get(); // wait.. if it ever get's here.. which it wont (well.. shouldn't) return 0; } Keep in mind that: 1) I'm no C++ guru. Actually, I'm quite horrible at it. 2) This is the first time I've even seen the Bloodshed C++ program (I couldn't get Visual Studio working to test your code, so I just installed what you said you were using :D) Other than that, I hope this helps at least a little. If you have any other issues or any trouble implementing anything I can try and help. But don't forget that I know very little about C++ :P. Quote Link to comment Share on other sites More sharing options...
h3%5kr3w Posted December 8, 2009 Author Share Posted December 8, 2009 THANK YOU SO MUCH!!! It worked perfectly. I cleaned up the code a little bit, found out that the random generators only go up to 32000 *I think*, so what I did was make an extra random generator, and then added them together for the wait time. I also hacked together some code to hide the window. Cheers! Blepper.c //Blepper, the act alike of the annoy-a-tron beeper :) //It has been done probably 1000x but this one was hacked together //by hexskrew @ hak5.org/forums with help from Kerberos (not teh kiteh, nor the encryption) #define _WIN32_WINNT 0x0500 #include <iostream> #include <windows.h> #include <ctime> using namespace std; int endr = 0; int main() { HWND hWnd = GetConsoleWindow(); //gets console window focus *I think* ShowWindow( hWnd, SW_HIDE ); //hides console window srand(time(0)) do { DWORD nWait1 = rand() % 325000; //nwait1 randomized to max 32.5 seconds DWORD nWait2 = rand() % 325000; //nwait2 randomized to max 32.5 seconds DWORD nWait = nWait1 + nWait2; //nWait = both randomized numbers put together DWORD nHz = rand() % 2000; //hz to be played DWORD nMs = rand() % 800; //ms of hz to be played if (nWait > 20000) //makes the timer a minimum of 20 seconds { Sleep(nWait); //sleep timer Beep(nHz, nMs); // x hertz (C5) for y milliseconds endr++; } } while(endr != 800); ShowWindow( hWnd, SW_SHOW ); //Shows console window cout << "HAHA! Gotcha! Have a great Christmas!" << '\n'; system("PAUSE"); return 0; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.