Results 1 to 12 of 12

Thread: [RESOLVED] question: how to generate random numbers

  1. #1

    Thread Starter
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Resolved [RESOLVED] question: how to generate random numbers

    hello,

    i know to use srand, rand funcs but they do not generate random numbers.

    basically i have 2 questions-

    1: does Dev C++ has randomize() and random() functions??

    2: do i have to loop again to check if the newly generated random number have been generated before, or is there any function present to do this?

    thank you.
    Show Appreciation. Rate Posts.

  2. #2
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: question: how to generate random numbers

    Code:
    #include <iostream>
    #include <conio.h>
    using namespace std;
    
    int main(){
    
    	srand(time(NULL));
        for(int i= 0; i<10; i ++){
    	cout<<rand()%10<<endl;
    }
        getch();
    	return 0;
    }

  3. #3

    Thread Starter
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: question: how to generate random numbers

    Jmacp,

    thank you for your response. i already know this. but have you tried your code?? it may not generate different random numbers. thats what my query is: do i have to store these numbers somewhere (like array) and loop to check if the newly generated number was also generated before.

    thank you.
    Show Appreciation. Rate Posts.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: question: how to generate random numbers

    rand() has no guarantee that it doesn't create the same number twice or more times in a row. The only guarantee it has is that, over time, the distribution of the numbers it generates is uniform over the range 0 <= n <= RAND_MAX. Doing a modulo 10 on this range will make repeats a lot more likely (and since 10 is neither a prime number nor a power of 2, it will also make the distribution non-uniform, which is when quality numbers are needed, a different method is used.)

    But if you want high-quality pseudorandom numbers, you should look into Boost.Random, which is also part of C++ TR1.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: question: how to generate random numbers

    Quote Originally Posted by CornedBee
    rand() has no guarantee that it doesn't create the same number twice or more times in a row. The only guarantee it has is that, over time, the distribution of the numbers it generates is uniform over the range 0 <= n <= RAND_MAX. Doing a modulo 10 on this range will make repeats a lot more likely (and since 10 is neither a prime number nor a power of 2, it will also make the distribution non-uniform, which is when quality numbers are needed, a different method is used.)

    But if you want high-quality pseudorandom numbers, you should look into Boost.Random, which is also part of C++ TR1.
    thanks, thats look promising.

    could you please explain how to use them?
    Show Appreciation. Rate Posts.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: question: how to generate random numbers

    Nope, never used them myself, so I don't know. Read the documentation.

    But there's a very simple example right at the page I linked.
    Code:
      boost::mt19937 rng;                 // produces randomness out of thin air
                                          // see pseudo-random number generators
      boost::uniform_int<> six(1,6)       // distribution that maps to 1..6
                                          // see random number distributions
      boost::variate_generator<boost::mt19937&, boost::uniform_int<> >
               die(rng, six);             // glues randomness with mapping
      int x = die();                      // simulate rolling a die
    All you need to do is replace the range with whatever range you want (that would be (0,9) in Jmacp's example).
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    Addicted Member SpS's Avatar
    Join Date
    Jul 2005
    Posts
    201

    Re: question: how to generate random numbers

    Quote Originally Posted by Jmacp
    Code:
    #include <iostream>
    #include <conio.h>
    using namespace std;
    
    int main(){
    
    	srand(time(NULL));
        for(int i= 0; i<10; i ++){
    	cout<<rand()%10<<endl;
    }
        getch();
    	return 0;
    }
    <conio.h> non-standard header
    getch() non-standard function
    and you need <ctime> for your code to compile
    #Appreciate others by rating good posts !!

    #The Software Peter Principle is in operation when unwise developers "improve" and "generalize" the software until they themselves can no longer understand it, then the project slowly dies.

    #People who are still ignorant of their ignorance are dangerous.

  8. #8

    Thread Starter
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: question: how to generate random numbers

    Quote Originally Posted by sunnypalsingh
    <conio.h> non-standard header
    getch() non-standard function
    and you need <ctime> for your code to compile
    thats 1 headache in Dev C++, it will compile the code even if <ctime> header file is not included!! (atleast Jmacp's code is running on my pc without <ctime>, but of course, not solving my problem)

    i wonder how and why??
    Show Appreciation. Rate Posts.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: question: how to generate random numbers

    Because standard headers can and do internally include each other. Thus, your particular standard library might include ctime from, say, iostream, and everytime you grab iostream you also get ctime.
    The problem is that you can't rely on this behaviour, because other standard libraries might do things differently.

    Another issue is that GCC is, due to legacy code still compiled with it, very forgiving regarding not declaring functions. You can call any function that returns int or void without ever declaring it, and GCC will merely emit a warning, and that only if you've got all warnings enabled.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10

    Thread Starter
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: question: how to generate random numbers

    thats what i thought of CB, but just wanted to confirm. thank you.

    all right then, i believe that either i have to use files from boost.org (as suggested by CB) or using the standard method of srand with loop checking all the previous generated random numbers.

    and still one question left: does Dev C++ has randomize() and random() functions??

    thank you.
    Show Appreciation. Rate Posts.

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: question: how to generate random numbers

    No, I'm pretty sure it doesn't.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12

    Thread Starter
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: question: how to generate random numbers

    Quote Originally Posted by CornedBee
    No, I'm pretty sure it doesn't.
    yeah, it seems so.

    thanks to all.
    Show Appreciation. Rate Posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width