#include <iostream>
#include <conio.h>
#include <string>
#include <windows.h>
#include <time.h>
usingnamespace std;
void GotoXY( int PosX, int PosY )
{
COORD p = { (SHORT)PosX, (SHORT)PosY };
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), p );
}
void Clear( int BackGroundColor=0, int ForeColor=1)
{
string strComand="color " + to_string(BackGroundColor) + to_string(ForeColor);
system(strComand.c_str() );
system("cls");
}
int randpos(int ranmax=100)
{
int r = rand() % ranmax +1;
return r;
}
int main()
{
//loop for draw text on screen on a random position
while(1)
{
srand(time(NULL));//reset the random
Clear(6,6);//clear the screen with a color using the system() function
GotoXY(randpos(),randpos());//change cursor position using SetConsoleCursorPosition() function
//and random values
cout << "hello world!!!"; //draw the text
if( GetKeyState(27) & 0x8000 ) break; //press ESC to exit
};
return 0;
}
but just seen the random function:
1 2 3 4 5
int randpos(int ranmax=100)
{
int r = rand() % ranmax +1;
return r;
}
these code works, but why the random values seems to be several times repeated?
You are currently calling srand(time(NULL)) every time that loop runs. By doing so, you're resetting your PRNG's seed to the current time every time that loop runs. Guess what happens when your loop runs several times a second?
seems that i found the error...
in function the max value is 100... but the standard window size is 120X30.... maybe that was the problem without notice
Note: rand() % N is almost never going to be perfectly uniform, for statistical reasons, as well as rand() possibly being a poor implementation (FurryGuy's post). But I'm assuming this isn't necessarily what's going on here... because usually the non-uniformity isn't *too* much.
Cambalinho, can you provide actual data to back the claim that a particular value is the result much more often than other values? dutch's post shows that it provides (more or less) a "good enough" result.
Edit: Just saw this, for some reason
Cambalinho wrote:
in function the max value is 100... but the standard window size is 120X30.... maybe that was the problem without notice
I figured it was something like that going on, and not an issue with the RNG itself.
now seems to work more efficient...
PS: some functions use '/' instead '%'.. always use the '%', because it's more efficient...
thank you
Repeater: why random.h is better than rand() or srand()?
The random number generation functions in the C++ random library have hard requirements on them to ensure they're actually good. I've got the standard open in front of me and it lays down a set of hard requirements in section 26, including some that I can but assume make some pretty solid enforcements.
The rand() function has no such restrictions, and historically has often been (and still is today) bloody awful (Albatross' OpenBSD above notwithstanding). For starters, you get a guarantee that the number generated will be between zero and 32767. What if you want random numbers up to 40000? You'll just have to hope that whoever wrote your rand() did a bit better. But you're just hoping.