Reliable Random Number Program

This is a discussion on Reliable Random Number Program within the C Programming forums, part of the General Programming Boards category; This is something that always bothered me in C; random numbers are really cumbersome in it. Is there a way ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    14

    Reliable Random Number Program

    This is something that always bothered me in C; random numbers are really cumbersome in it. Is there a way to generate random numbers within a certain range, as in a random number between 1 and 10? I think it'd be a useful thing to know.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Yes. We covered this not too long ago in random number. You should also read Prelude's article on using rand().
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    accually there is a way using this:

    Code:
    int x;
    x = low_limit + (rand()%(hight_limit - low_limit));
    i think this is correct but anyway i' ll check this tommorow for sure...

  4. #4
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Randomness is impossible. This is how you generate a pseudorandom integer between one and ten.

    Code:
    srand(time(NULL));
    int x = (rand() % 10) +1;
    This assigns x to a pseudorandom integer between zero and nine and adds one to it.
    Last edited by Babkockdood; 09-02-2010 at 03:29 PM.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    here is my code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void){
    	int r, i;
    	float x;
    
    		printf("Type a number: \n");
    		scanf("%d", &r);
    		srand(r);
    			for(i=0; i<50; i++){
    				x = 10 + (rand() % (20-10)); // x = low_limit + rand() % (hight_limit - low_limit));
    				printf("x: %.0f \n", x);
    			}
    return 0;
    }
    this prints numbers between [10 - 20]...
    i hope i helped!
    Last edited by brack; 09-02-2010 at 04:02 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Quote Originally Posted by brack
    i think this is correct but anyway i' ll check this tommorow for sure...
    It is correct, but may introduce bias. You should likewise read the links that I provided.

    Quote Originally Posted by Babkockdood
    Randomness is impossible.
    That depends on what you define as "randomness". User Name:'s post #13 in the thread that I linked to provides an example of a possible source of randomness. Your example is another example of what brack described.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Quote Originally Posted by laserlight View Post
    It is correct, but may introduce bias. You should likewise read the links that I provided.
    I took a look at them, but i found them somehow difficult because i am still learning C...
    so i tried so give an example...i hope i helped and not make somebody nervous...

  8. #8
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by laserlight View Post
    That depends on what you define as "randomness". User Name:'s post #13 in the thread that I linked to provides an example of a possible source of randomness. Your example is another example of what brack described.
    Psuedorandom number generation in computers takes input from the system that isn't very predictable and puts it through a huge mathematical equation to generate a number that appears random.

    Randomness in general, is impossible. Think about it. If you roll dice, the result depends on how you rolled it and what face pointed up when you rolled it. If you flip a coin, the result depends on how you flipped it and what face pointed up originally. If you pick something out of a hat, the result depends on where everything is in the hat and where you decide to pick it.

    I once ran the snippet of code I posted with a simple printf statement. I ran the program once every second and the output looked like this.

    Code:
    Random number: 7
    Random number: 14
    Random number: 28
    Random number: 35
    Random number: 42
    "Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin."
    ~John von Neumann

  9. #9
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    My opinion: I am a programmer...i mean i want to be someday and i write down C code, so when i hear "randomness" i think "numbers" without a specific sequence...in fact i don' t care what "randomness" means in the "real word"...Oh, also the next words are the same for me randomness ~ psuedorandom...

  10. #10
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by Babkockdood View Post
    Randomness in general, is impossible.
    Well. Depends on the source. From what I understand, atmospheric noise, radioactive decay and
    background radiation are very good sources of randomness.

    NaE, tho.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Quote Originally Posted by Babkockdood
    Psuedorandom number generation in computers takes input from the system that isn't very predictable and puts it through a huge mathematical equation to generate a number that appears random.
    EDIT: frankly, you are not clear. The random aspect is the "input from the system that isn't very predictable". The "huge mathematical equation" (which is not necessarily "huge") sounds like you are indeed talking about a pseudorandom number generator, where the random input is converted to a number that is used merely as a seed. But if the random input is itself converted directly to a random number, then we call the generator a random number generator, not a pseudorandom number generator. You can disagree on philosophical grounds, but practicality trumps philosophy here.

    Quote Originally Posted by Babkockdood
    Randomness in general, is impossible. Think about it. If you roll dice, the result depends on how you rolled it and what face pointed up when you rolled it. If you flip a coin, the result depends on how you flipped it and what face pointed up originally. If you pick something out of a hat, the result depends on where everything is in the hat and where you decide to pick it.
    This is why I say that it depends on what you define as "randomness". One can use philosophical notions of randomness, or one can use notions of randomness that have practical applications, e.g., statistical randomness, unpredictability, and lack of reliable reproduction. For the latter, the roll of a fair die might be regarded as random, even if one might argue that it is deterministic. Likewise, one might make use of the theoretical randomness of radioactive decay, even if "God does not play dice with the universe".

    Quote Originally Posted by Babkockdood
    I once ran the snippet of code I posted with a simple printf statement. I ran the program once every second and the output looked like this.
    And your point is?

    EDIT:
    Hold on, you "ran the program once every second"? That is absurd: to make good use of a pseudorandom number generator, you should be printing out a sequence from the same seed, not continually reseeding at one second intervals and taking the first number in each sequence. Your failure to use a PRNG correctly calls into question your own skills, not the quality of the PRNG.
    Last edited by laserlight; 09-03-2010 at 04:47 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    If anyone is interested, I've crated a DLL based on Prelude's article. You can download the source here.

    I've tested it on Win7 and WinXp. Any feedback would be greatly appreciated.
    Last edited by msh; 09-04-2010 at 07:09 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random number gen & looping probelm :?
    By FoxeySoulSLayer in forum C++ Programming
    Replies: 1
    Last Post: 04-10-2009, 05:44 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  4. Generating a random number?
    By Konspiracy in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 12:33 AM
  5. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21