<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Zufallszahlen: gleichverteilt, normalverteilt, dreiecksverteilt]]></title><description><![CDATA[<p>in Abhängigkeit von d soll die Funktion unterschiedlich verteilte Zufallszahlen erzeugen:</p>
<pre><code>float OutputParams::ranx(int d, float m, float s)
{
  float rv;
 // d = Art der Verteilung:
 // 0 : Wert zw. m-s und m+s (gleichverteilt)
 // 1 : Wert normalverteilt um m mit s=s
 // 2 : Dreiecksverteilung

  switch(d)
  {
    case 0 :
      rv = 2.0*(float)rand()/(float)RAND_MAX-1.0;  // Wert zw.  -1 und   1 (gleichverteilt)
      rv = m+s*rv;                                 // Wert zw. m-s und m+s (gleichverteilt)
      break;
    case 1 :
      rv = ranNorm();                          // Wert normalverteilt um Null mit s=1
      rv = m+s*rv;                                 // Wert normalverteilt um m  mit s=s
      break;
    case 2 :
      rv = -1;                                     // Wert dreiecksverteilt
      break;
    default :
      rv = -2;                                      // Wert = m, d.h. const. - keine Verteilung
      break;
    }
  return(rv);
}
</code></pre>
<p>Normalverteilt hab ich:</p>
<pre><code>float OutputParams::ranNorm()    //N(0,1), d.h. Wert normalverteilt um Null mit s=1
{
  static int   iset=0;
  static float gset;
  float rnum;
  float fac;
  float rsq;
  float v1,v2;

  if ( iset == 0 )
    {
      do {
	v1  = 2.0*(float)rand()/(float)RAND_MAX-1.0;
	v2  = 2.0*(float)rand()/(float)RAND_MAX-1.0;
	rsq = v1*v1+v2*v2;
      } while ( rsq &gt;= 1.0 || rsq == 0.0);

      fac = sqrt(-2.0*log(rsq)/rsq);
      rnum = v1*fac;
      gset = v2*fac;
      iset = 1;
    }
  else
    {
      rnum = gset;
      iset = 0;
    }
  return rnum;
}
</code></pre>
<p>Kann mir jemand sagen, wie ich dreiecksverteilte Zufallszahlen generieren kann?</p>
<p>mfG robi1806</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/86225/zufallszahlen-gleichverteilt-normalverteilt-dreiecksverteilt</link><generator>RSS for Node</generator><lastBuildDate>Fri, 03 Jul 2026 16:34:57 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/86225.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 17 Sep 2004 09:05:42 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Zufallszahlen: gleichverteilt, normalverteilt, dreiecksverteilt on Fri, 17 Sep 2004 09:05:42 GMT]]></title><description><![CDATA[<p>in Abhängigkeit von d soll die Funktion unterschiedlich verteilte Zufallszahlen erzeugen:</p>
<pre><code>float OutputParams::ranx(int d, float m, float s)
{
  float rv;
 // d = Art der Verteilung:
 // 0 : Wert zw. m-s und m+s (gleichverteilt)
 // 1 : Wert normalverteilt um m mit s=s
 // 2 : Dreiecksverteilung

  switch(d)
  {
    case 0 :
      rv = 2.0*(float)rand()/(float)RAND_MAX-1.0;  // Wert zw.  -1 und   1 (gleichverteilt)
      rv = m+s*rv;                                 // Wert zw. m-s und m+s (gleichverteilt)
      break;
    case 1 :
      rv = ranNorm();                          // Wert normalverteilt um Null mit s=1
      rv = m+s*rv;                                 // Wert normalverteilt um m  mit s=s
      break;
    case 2 :
      rv = -1;                                     // Wert dreiecksverteilt
      break;
    default :
      rv = -2;                                      // Wert = m, d.h. const. - keine Verteilung
      break;
    }
  return(rv);
}
</code></pre>
<p>Normalverteilt hab ich:</p>
<pre><code>float OutputParams::ranNorm()    //N(0,1), d.h. Wert normalverteilt um Null mit s=1
{
  static int   iset=0;
  static float gset;
  float rnum;
  float fac;
  float rsq;
  float v1,v2;

  if ( iset == 0 )
    {
      do {
	v1  = 2.0*(float)rand()/(float)RAND_MAX-1.0;
	v2  = 2.0*(float)rand()/(float)RAND_MAX-1.0;
	rsq = v1*v1+v2*v2;
      } while ( rsq &gt;= 1.0 || rsq == 0.0);

      fac = sqrt(-2.0*log(rsq)/rsq);
      rnum = v1*fac;
      gset = v2*fac;
      iset = 1;
    }
  else
    {
      rnum = gset;
      iset = 0;
    }
  return rnum;
}
</code></pre>
<p>Kann mir jemand sagen, wie ich dreiecksverteilte Zufallszahlen generieren kann?</p>
<p>mfG robi1806</p>
]]></description><link>https://www.c-plusplus.net/forum/post/608907</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/608907</guid><dc:creator><![CDATA[robi1806]]></dc:creator><pubDate>Fri, 17 Sep 2004 09:05:42 GMT</pubDate></item></channel></rss>