[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



Egon Andersen, Talura wrote:
sslug@sslug wrote:

Hej c-liste

 Jeg skal lave en sandsynligheds-funktion, der givet et tal imellem 0 og
 100, returnerer 1 med <tal>% sandsynlighed, og 0 med <100 - tal>%
 sandsynlighed.

dvs. givet tallet 40 skal funktionen returnere 1 40% af gangene, og 0 60%
af gangene, funktionen bliver kaldt.


Har I nogle gode ideer til, hvordan det kan lade sig gøre?

 Med venlig hilsen
 Marie Louise




se: man rand


Den giver dig et pseudo-tilfældigt tal mellem 0 og RAND_MAX
Så er det blot at lave en spørgesætning a la:

if(tal < RAND_MAX x 40%) return 1;
else return 0;

/Egon




Uups! Burde vel egentligt være:

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

men jeg håber du fangede ideen.

/Egon
--
* Talura ApS      * Phone: +45 43 52 50 00 *
* Baldershøj 24 B * mailto:sslug@sslug  *
* DK-2635  Ishøj  * http://www.talura.dk   *



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