#1
  1. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95

    good random function


    Well, i have come to realize that the randomize playlist function in the XMMS player sucks ***. So i got the source and looked at it, and now i see why. I want to rewrite it so that it never will have randomize the list and have 2 songs that were adjacent be adjacent after randomization. Here is the source for the shuffle function:
    Code:
    static GList *playlist_shuffle_list(GList *list)
    {
    	/* Caller should holde playlist mutex */
    	/*
    	 * Note that this doesn't make a copy of the original list.
    	 * The pointer to the original list is not valid after this
    	 * fuction is run.
    	 */
    	gint len = g_list_length(list);
    	gint i, j;
    	GList *node, **ptrs;
    
    	if (!len)
    		return NULL;
    
    	ptrs = g_new(GList *, len);
    
    	for (node = list, i = 0; i < len; node = g_list_next(node), i++)
    		ptrs[i] = node;
    
    	j = random() % len;
    	list = ptrs[j];
    	ptrs[j]->next = NULL;
    	ptrs[j] = ptrs[0];
    
    	for (i = 1; i < len; i++)
    	{
    		j = random() % (len - i);
    		list->prev = ptrs[i + j];
    		ptrs[i + j]->next = list;
    		list = ptrs[i + j];
    		ptrs[i + j] = ptrs[i];
    	}
    	list->prev = NULL;
    
    	g_free(ptrs);
    
    	return list;
    }
    i was thinking of perhaps getting the seed from the clock. but if i remember correctly someone else on here a while ago was saying there are better methods. anyone have any ideas?
    ps. i would like to have it prompt the user for a mouse movement and then do something with the start and enpoints, but i only know how to do that in win32 and C++ . i've never actually programmed a C program.
  2. #2
  3. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    this is far from a final answer but just thought i'd chuck this in:

    from this pdf http://islab.oregonstate.edu/koc/ece...s/bulletn1.pdf

    One essential ingredient in producing good random
    numbers in software, then, is to use a good PRNG. (psuedo random number generator)
    Important to note is that although the PRNG may
    produce statistically good looking output, it also has
    to withstand analysis to be considered strong. Since
    the one included with your compiler or operating
    system may or may not be, we recommend you don?t
    use it. Instead, use a PRNG that has been verified
    to have a high degree of randomness. RSA?s BSAFE
    toolkit uses the MD5 message digest function as a
    random number generator. BSAFE uses a state value
    that is digested with MD5. The strength of this approach
    relies on MD5 being a one-way function ?
    from the random output bytes it is difficult to determine
    the state value, and hence the other output
    bytes remain secure. Similar generators can be constructed
    with other hash functions, such as SHA1.


    i'd like to know some good code to get a random number out of mouse jiggles. i always thought maybe you could collect the mouse movements to use for the number continually, rather than asking the user to jiggle your mouse round for half a minute now, like pgp does

    interestingly, the PGP 8 source code for mac and windwos is available for download on the pgp site. might be worth looking into? http://www.pgp.com/products/sourcecode.html
  4. #3
  5. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    Originally posted by balance

    i'd like to know some good code to get a random number out of mouse jiggles. i always thought maybe you could collect the mouse movements to use for the number continually, rather than asking the user to jiggle your mouse round for half a minute now, like pgp does
    http://www.pgp.com/products/sourcecode.html
    -thanks for the links, i'll check them out. as for the above quote, i was thinking of a few different things u could do. one could be to have the user push down the button and draw an imaginary line to some other place on the screen and then release the mouse button at this point. when they first click the button down u could get the system time and wehn they release u could get the system time. then use this in combination with the distance traveled from downclick to upclick and calculate the velocity(think that's right word, damn i havent had physics in a while) and use that for the seed. hmm,i think i will try that out and see how it works.
  6. #4
  7. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    i tried my above theory. it works alright, the numbers have a pretty large range. here is the pertinent code:

    Code:
    case WM_LBUTTONDOWN :
           // x = LOWORD (lParam);
           // y = HIWORD (lParam)
           // wParam: shift, ctrl key and button flags
    
    	   tic1 = GetTickCount();
    	   x = LOWORD (lParam);
    	   y = HIWORD (lParam);
           InvalidateRect (hWnd, NULL, TRUE);
           return 0;
    
      case WM_LBUTTONUP:
    	   
    	   tic2 = GetTickCount();
    	   x1 = LOWORD (lParam);
    	   y1 = HIWORD (lParam);
    	   
    	   ticR = (tic2 - tic1)/100;
    	   
    	   dist = 1000 * sqrt((pow((y-y1),2)) + (pow((x - x1),2))); 
    	
    	   if(ticR!=0)
    		result = dist/ticR;
    	   sprintf(txt,"The velocity was %u ", result);
    	   text = txt;  
    
    	   MessageBox(hWnd,text,"dist",MB_OK);
    
    	   InvalidateRect (hWnd, NULL, TRUE);
           return 0;
    - i was thinking a better idea might be to WM_MOUSEMOVE or whatever it is that responds to mouse movement. and each movement increment a distance counter, and then when the user clicks down the button have that signify the "end of movement" and do some calculations from there involving the total distance travelled and time it took...
  8. #5
  9. Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Location
    Italy -> Naples
    Posts
    9
    Rep Power
    0

    Lightbulb Other code


    It's a good idea to use mouse's movements to generate randomn numbers, this system is used also by some version PGP.

    Some months ago I wrote this code for a linux game, it's simple but works fine:

    Code:
    #include <time.h>
    #include <sys/time.h>
    
    unsigned long int next=1;
    
    short int k=1;
    
    int myrand(int a, int b)
    {
            struct timeval t1;
    
            int temp;
    
            gettimeofday(&t1,NULL);
    
            if(k>5)
            {
                    k=1;
                    next=1;
            }
    
            next= (next * t1.tv_usec * t1.tv_usec) + (t1.tv_sec/100);
    
            k++;
    
            if(t1.tv_usec==0)
                    temp=1;
            else
                    temp=t1.tv_usec;
    
            return (unsigned int) (((next/temp)%b)+a);
    }
    If are u looking for other resources u can give a look at:
    http://www.library.cornell.edu/nr/bookcpdf.html
  10. #6
  11. No Profile Picture
    Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Springfield, USa at Homer's pad kicking it with my girl Marge
    Posts
    4
    Rep Power
    0
    great random function...
  12. #7
  13. No Profile Picture
    Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Springfield, USa at Homer's pad kicking it with my girl Marge
    Posts
    4
    Rep Power
    0

    Re: Other code


    Originally posted by M3xican
    It's a good idea to use mouse's movements to generate randomn numbers, this system is used also by some version PGP.

    Some months ago I wrote this code for a linux game, it's simple but works fine:

    Code:
    #include <time.h>
    #include <sys/time.h>
    
    unsigned long int next=1;
    
    short int k=1;
    
    int myrand(int a, int b)
    {
            struct timeval t1;
    
            int temp;
    
            gettimeofday(&t1,NULL);
    
            if(k>5)
            {
                    k=1;
                    next=1;
            }
    
            next= (next * t1.tv_usec * t1.tv_usec) + (t1.tv_sec/100);
    
            k++;
    
            if(t1.tv_usec==0)
                    temp=1;
            else
                    temp=t1.tv_usec;
    
            return (unsigned int) (((next/temp)%b)+a);
    }
    If are u looking for other resources u can give a look at:
    http://www.library.cornell.edu/nr/bookcpdf.html
    yes it is...

IMN logo majestic logo threadwatch logo seochat tools logo