Random Numbers in C

Discussion in 'Programming' started by fex, Mar 16, 2010.

  1. #1
    Hello,

    I've found a lot on the internet about generating random numbers but none of it really does make sense to me, since I'm new to C.
    All I need is to generate a random number from 0 till 2. So 0,1 or 2 should be the outcome. From what I've been reading, the rand() function in stdlib.h would always give me the same number, is it true? I would just do this...

    #include <stdlib.h>
    
    int x;
    
    void main(void){
    while(1){
    ...
    x=rand();
    ...
    }
    }
    
    Code (markup):

     
    fex, Mar 16, 2010 IP
  2. wineri.es

    wineri.es Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
  3. NeoCambell

    NeoCambell Peon

    Messages:
    456
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
  4. devilsonline

    devilsonline Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    try this one
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h> // for time()


    // Generate a random number between nLow and nHigh (inclusive)
    unsigned int GetRandomNumber(int nLow, int nHigh){

    return (rand() % (nHigh - nLow + 1)) + nLow;
    }


    void main(){

    int i;

    srand(time(0)); // set initial seed value to system clock

    // Generate 100 random numbers
    for (i = 0; i < 100; i++){
    printf("%d\t", GetRandomNumber(0, 3));
    }
    }
     
    devilsonline, Mar 21, 2010 IP
  5. fex

    fex Peon

    Messages:
    89
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks, just what I need. One problem, I do not have time.h but only timers.h, is time() included in timers.h as well?
     
    fex, Mar 23, 2010 IP
  6. NeoCambell

    NeoCambell Peon

    Messages:
    456
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #6
    time.h is a standard header file that comes with almost every C/C++ compiler package I know. Double check that.
     
    NeoCambell, Mar 23, 2010 IP
  7. blueleaf

    blueleaf Active Member

    Messages:
    112
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #7
    It is always possible to open the timers.h and see the functions included in it. :)
     
    blueleaf, Mar 23, 2010 IP
  8. fex

    fex Peon

    Messages:
    89
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I've installed Microchip C18 Compiler and I double checked, no time.h is in the h folder.

    Opening the file: nearly only defines are in it... no time() function.

    EDIT: Can't I use anything else than the time? Would it not work if I just use a timer (which counts milliseconds) as seed (it's already present in my program)? Are there any limits concerning the seed?
     
    Last edited: Mar 23, 2010
    fex, Mar 23, 2010 IP
  9. magam

    magam Active Member

    Messages:
    151
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #9
    is your problem solved? you got solution?
     
    magam, Mar 23, 2010 IP
  10. NeoCambell

    NeoCambell Peon

    Messages:
    456
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #10
    NeoCambell, Mar 24, 2010 IP
  11. fex

    fex Peon

    Messages:
    89
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Sorry NeoCambell, I didn't know it was called that way. As before, I'm new to this :D But yes, that's what I need I guess..

    So, changing following line of code would give me a number being 0, 1 or 2:

    return genrand_int32()*(2.0/4294967295.0); // [0,1]*2 => [0,2]

    Thanks !
     
    fex, Mar 24, 2010 IP
  12. sanzal

    sanzal Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    C ??? after B and before D .....:eek:
     
    sanzal, Mar 24, 2010 IP
  13. fex

    fex Peon

    Messages:
    89
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #13
    The fact that it is called an 'embedded system'

    geez.. ;)
     
    fex, Mar 25, 2010 IP