jeudi 16 juillet 2015

Where does rand() get its numbers from?

while working on a little project i thought i could generate "random" filenames with a little bit of code like this,

std::cout << "image"+rand()%255 << std::endl;

The outputs i got made no sense to me. They seem to be random parts of error messages.

for example this piece of code:

int main()
{
    while(1){
        std::cout << "image" + rand() % 255 << std::endl;
    }
    return 0;
}

produces gibberish like:

> ge
>
> n
>
>
> i
>
>
> ring too long
>
> U
>
>
>
>
>
> &
>
> n
> _
> o
>  string position
> e
> lid string position
> i
>
>
>
>
> U
> g
> invalid string position
>
> U
> ing position
>
>
> &
>
>
>
>
> ring position
> !
> n
>
> oo long
>
>
>
>
>
> o
> position

and a more complex piece of code in QtCreator (with the same cout rand endl statement in the main loop)

>    atform\mainwindow.cpp:210
>0
>I , null image received
>indow.cpp:210
>(QImage)
>dImage(QImage)
>, error: image not read from file!
> updatePlayerUI , null image received
>updatePlayerUI(QImage)
>ow.cpp:210
>dImage(QImage)
>ot chosen
>s not chosen
>og, error: image not read from file!
> was not chosen
>age not read from file!
>r: image not read from file!
>neDataPlatform\mainwindow.cpp:210
>error: image not read from file!

what is the reason for this?




Aucun commentaire:

Enregistrer un commentaire