Random numbers

I tried to generate random ints with this code:


#include

#include


void main()

{

int rand;

rand=random(10);

cout<<rand;<br>
}


but DJGPP gives me an error in stdlib.h at line 111,

something like: too many arguments.


What is wrong or is there any other ways to get random numbers?


Rasmus

rasmus.paivarinta@usa.net


Comments

  • : I tried to generate random ints with this code:


    : #include

    : #include


    : void main()

    : {

    : int rand;

    : rand=random(10);

    : cout<<rand;<br>
    : }


    : but DJGPP gives me an error in stdlib.h at line 111,

    : something like: too many arguments.


    random doesn't take any numbers as arguments. What you want is:


    rand=random()%10;


    which will give you a random number between 0 and 9 inclusively.




    I highly recommend using the rand() function if you're interested in this working on several compilers. So instead, name your variable something else like x, and then:


    x=rand()%10;




    That's just a suggestion, though.




  • Hey Thanks a lot!

    I would also like to call a fuction that generates

    different numbers everytime I run the program.


    I tried to just put randomize(); to the beginning, but DJGPP gave an error.


    Rasmus




  • : I would also like to call a fuction that generates

    : different numbers everytime I run the program.

    That will, unfortunately, mean that any bugs you run into may be unrepeatable. However, if you use rand() instead of random(), then use srand(value) to see rand() with a particular value at the start.


    Here's a typical trick:


    /* Seed the random-number generator with current time so that

    * the numbers will be different every time we run. */

    srand( (unsigned)time( NULL ) );





  • Would you like to write a code that generates

    different number everytime I run the program for

    me, please?


    Razze


  • : Would you like to write a code that generates

    : different number everytime I run the program for

    : me, please?


    : Razze




    I just did:


    /* Seed the random-number generator with current time so that

    * the numbers will be different every time we run. */


    srand( (unsigned)time( NULL ) );



    /*That, and for the actual random number:*/


    int x=rand();





  • DJGPP says:

    "time" can not be used as a function

    Whats wrong?


    Razze


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories