[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 writes:

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

Dit problem er at tal/100 laver en heltalsdivision fordi både tal og
100 er heltal. Hvis du istedet skriver 'tal/100.0' vil den
formodentligt gøre som du forventer.

pma-20% cat foo.c 
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {

    int i;
    for(i=0; i < 10000; i++) {
        printf("%d\n",(rand() <= ((atoi(argv[1])/100.0) * RAND_MAX))? 1 : 0);
    }
}
pma-20% gcc foo.c   
pma-20% ./a.out 50 | perl -ne 'chomp; $n++; $h{$_}++; END{printf "0: %d (%d%%)\n1: %d (%d%%)\n", $h{0}, ($h{0}/ $n) * 100, $h{1}, ($h{1}/$n) * 100}'
0: 4964 (49%)
1: 5036 (50%)
pma-20% ./a.out 70 | perl -ne 'chomp; $n++; $h{$_}++; END{printf "0: %d (%d%%)\n1: %d (%d%%)\n", $h{0}, ($h{0}/ $n) * 100, $h{1}, ($h{1}/$n) * 100}'
0: 2935 (29%)
1: 7065 (70%)
pma-20% 

-- 
SSLUGs julekalender - hver dag fra den 1. til den 24. december
   http://www.sslug.dk/julekalender/


 
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] *