Callback function in C, but problem with rand() and srand() function

This is a discussion on Callback function in C, but problem with rand() and srand() function within the C Programming forums, part of the General Programming Boards category; i've just write a code test, but rand() and srand() seem not to be work. Please show me what happens! ...

  1. #1
    Registered User PuKa's Avatar
    Join Date
    Oct 2011
    Posts
    23

    Callback function in C, but problem with rand() and srand() function

    i've just write a code test, but rand() and srand() seem not to be work. Please show me what happens! . thanks a lot.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int cmp(void const* a, void const* b) { return ( *(int*)a - *(int*)b ); }
    int main() {
        srand( (unsigned int) time(NULL) ); int ar[10],i;
        // fill array
        for (i = 0; i < 10; ++i)
            ar[i] = rand()%20+1; // range (1->20)
        // print before sort
        for (i = 0; i < 10; ++i)
            printf("%d ", i);
        // quick sort
        qsort( (void*)ar, 10, sizeof(int), cmp );
        // print after sort
        printf("\n");
        for (i = 0; i < 10; ++i)
            printf("%d ", i);
        return 0;
    }
    the output not to be changes once i run program, the output is fixed:
    0 1 2 3 4 5 6 7 8 9
    0 1 2 3 4 5 6 7 8 9
    Last edited by PuKa; 11-14-2011 at 05:58 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Beware what you are printing:

    Code:
        // print before sort
        for (i = 0; i < 10; ++i)
            printf("%d ", i);
        for (i = 0; i < 10; ++i)
        // print after sort
            printf("%d ", i);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User PuKa's Avatar
    Join Date
    Oct 2011
    Posts
    23
    Oh, god, that's supprise! . Thanks much and more.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. callback function
    By koszta in forum C Programming
    Replies: 2
    Last Post: 02-23-2009, 11:51 AM
  2. Use srand() twice in a function?
    By Jake.c in forum C Programming
    Replies: 5
    Last Post: 01-21-2009, 11:51 AM
  3. callback function v. regular function
    By 7stud in forum C++ Programming
    Replies: 8
    Last Post: 03-24-2005, 10:34 AM
  4. Callback function???
    By ripper079 in forum C++ Programming
    Replies: 3
    Last Post: 11-14-2002, 08:29 PM
  5. Pointer to member function as callback function
    By ninebit in forum C++ Programming
    Replies: 10
    Last Post: 03-01-2002, 04:52 AM

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