C++ random number generator

Does it exist any random generators? I saw one for some time ago done with conio.h, but it didn't work at all. If you have one, mail it to me or DCC send it to me on IRC. My nick is M3ntos, i am on #odalen & #cybervenner on Efnet.

I'm always open for suggestions and new projects.


URL:http://home.sol.no/~troub/

Comments

  • Syntax



    #include

    int random(int num);



    Description



    Random number generator.

    random returns a random number between 0 and (num-1). random(num) is a macro defined in stdlib.h. Both num and the random number returned are integers.



    Return Value



    random returns a number between 0 and (num-1).



    Copy/Pasted From Borland C++ 5.0 Programmer's Guide




  • It should be #include <stdlib.h>



    So that you include stdlib.h, due to HTML formating it was removed from my last message...




  • : Does it exist any random generators? I saw one for some time ago done with conio.h, but it didn't work at all. If you have one, mail it to me or DCC send it to me on IRC. My nick is M3ntos, i am on #odalen & #cybervenner on Efnet.



    What do you expect us to contact you personally? Get real.



    The following example is ANSI C/C++ standard. Null and Void's example will only work on Borland C++ compilers.





    #include < stdio.h >

    #include < stdlib.h >



    int main ()

    {

    int nNumber;



    nNumber = rand() % 100; // Generate a number from 0 to 99.



    nNumber = rand() % 13 // Generate a number from 0 to 12.



    ...

    return 0;

    }





    -Xotor-


  • Oh yeah and if you want to randomize the rand() function's seed number (for truly randomness), use this function:



    #include stdlib.h

    #include time.h



    time_t t;



    srand((unsigned) time(&t));



    -Xotor-


  • : Does it exist any random generators? I saw one for some time ago done with conio.h, but it didn't work at all. If you have one, mail it to me or DCC send it to me on IRC. My nick is M3ntos, i am on #odalen & #cybervenner on Efnet.

    : I'm always open for suggestions and new projects.

    :



    #include

    #include

    #include



    /******************************************************************************/

    /******************* Random number generator **********************************/



    get_rand_number(int begin, int end)

    {

    float x;

    srand((int)time(NULL));

    x=(float)begin+((float)end*rand()/(RAND_MAX+1.0));

    x=(int)x;

    return(x);

    }

    This works for me.

    try it






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