[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [CPROG] 'sandsynligheds'-funktion



sslug@sslug wrote:
Tusind tak for forslagene,

men uanset om jeg benytter:

if ((tal/100) > (rand()/RAND_MAX)) return 1;
else return 0;

eller:

if(rand() < RAND_MAX * tal/100) return 1; else return 0;

returnerer funktionen stort set altid 0!

Hvis jeg ændrer på funktionerne fx. und lader at dividere med 100 svarer
funktionen altid 1, medmindre tal=0.

Hvis jeg dividerer med 50 istedet for 100 svarer funktionen 0 for
halvdelen og 1 for resten.

Men uanset disse ændringer, svarer funktionen det samme, for det samme
'tal'. Det vil sige, der er 100% sandsynlighed for det samme svar og ikke
tallets værdi procent sandsynlighed, som det var meningen.


Med venlig hilsen Marie Louise




Du får selvfølgelig problemer, hvis 'tal' er defineret af typen 'int'.
Derimod skulle det fungere fint, hvis 'tal' er af typen 'float'.
Det har noget med værdiområdet for 'int' i forhold til RAND_MAX at gøre.
(RAND_MAX er tæt på INTMAX)

Du kan måske også være interesseret i at kunne gentage dine udfald, hvilket du kan sikre ved at sætte et start punkt for din random-generator ved brug af srand(). Men det har du selvfølgelig læst i 'man rand'

/Egon



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:09 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *