Results 1 to 2 of 2

Thread: How to make random generated numbers?

  1. #1
    Sergeant EvoloZz's Avatar
    Join Date
    Sep 2012
    Location
    Helsinki, Finland
    Posts
    360
    Thanks
    314
    Thanked 167 Times in 120 Posts

    How to make random generated numbers?

    Its kinda annoying when rand() function always gives 41 as a random number, and if I for example have
    Code:
    while(i<5)
    (list of 5 random numbers), the numbers are always: 41, 18467, 6334, 26500, 19169. Here is the whole thing:
    Code:
        int i=0;
        
        while(i<5){
        printf("%d\n", rand());
        i++;
        }
    So I want the program to truly generate random numbers not same numbers everytime -.-


    Thanks for help already.

    ~EvoloZz

  2. #2
    Assadministrator kung foo man's Avatar
    Join Date
    Jun 2012
    Location
    trailerpark
    Posts
    2,010
    Thanks
    2,102
    Thanked 1,084 Times in 753 Posts
    Just call srand() with some timestamp first:

    Code:
    #include <time.h>
    
    
    
    srand ( time(NULL) );
    srand() is "mixing" the numbers, so they wont be the same next start.
    timescale 0.01

  3. The Following User Says Thank You to kung foo man For This Useful Post:

    EvoloZz (15th December 2012)

Posting Permissions

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