Software Design
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreSoftware Design

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 15th, 2003, 12:23 AM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 94
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.

Reply With Quote
  #2  
Old April 15th, 2003, 12:54 PM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 11
this is far from a final answer but just thought i'd chuck this in:

from this pdf http://islab.oregonstate.edu/koc/ec...bs/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

Reply With Quote
  #3  
Old April 15th, 2003, 04:34 PM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 94
Quote:
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.

Reply With Quote
  #4  
Old April 15th, 2003, 11:12 PM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level)infamous41md User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 94
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...

Reply With Quote
  #5  
Old April 28th, 2003, 05:07 PM
M3xican's Avatar
M3xican M3xican is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Location: Italy -> Naples
Posts: 9 M3xican User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to M3xican
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

Reply With Quote
  #6  
Old May 3rd, 2003, 12:04 AM
chalieboy chalieboy is offline
Banned
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: Springfield, USa at Homer's pad kicking it with my girl Marge
Posts: 4 chalieboy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
great random function...

Reply With Quote
  #7  
Old May 3rd, 2003, 01:29 AM
chalieboy chalieboy is offline
Banned
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Location: Springfield, USa at Homer's pad kicking it with my girl Marge
Posts: 4 chalieboy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Re: Other code

Quote:
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...

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreSoftware Design > good random function

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap