<?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[Zufallszahl mit Gewichtung]]></title><description><![CDATA[<p>Basierten auf meine <a href="http://www.c-plusplus.net/forum/306196" rel="nofollow">Frage</a> hab ich das gebastelt:</p>
<pre><code class="language-cpp">#include &quot;FreeFunktion.h&quot;	//console::DontClose();
#include &lt;iostream&gt;
#include &lt;vector&gt;
using namespace std;

int getRandomNumber(int Number, unsigned Sigma) {
	if(Sigma&lt;2) return Number;
	double P = (double)rand() / RAND_MAX;
	double ItemCount = Sigma*Sigma;
	double ItemsForNextBound = 2;
	double nextBound = ItemsForNextBound/ItemCount;
	int Spread=1;

	while(P &gt;= nextBound) {
		Spread++;
		ItemsForNextBound+=2*(Spread);
		nextBound = ItemsForNextBound/ItemCount;
	}

	if(rand()%2)
		return Number + (Sigma - Spread);
	return Number - (Sigma - Spread);
}

int main() {
	const unsigned Count=1E6;
	double sum=0;
	vector&lt;double&gt; Probs(21);
	cout&lt;&lt;&quot;Test 1: \n&quot;;
	for(unsigned i=0; i&lt;Count; ++i)
		Probs[getRandomNumber(20, 4)-10]++;

	for(unsigned i=0; i&lt;Probs.size(); ++i) {
		cout&lt;&lt;Probs[i]/Count&lt;&lt;&quot;\n&quot;;
		sum+=Probs[i];
	}
	cout&lt;&lt;&quot;Sum: &quot;&lt;&lt;sum/Count;

	cout&lt;&lt;&quot;Test 2: \n&quot;;
	for(unsigned i=0; i&lt;Count; ++i)
		Probs[getRandomNumber(20, 10)-10]++;

	for(unsigned i=0; i&lt;Probs.size(); ++i) {
		cout&lt;&lt;Probs[i]/Count&lt;&lt;&quot;\n&quot;;
		sum+=Probs[i];
	}
	cout&lt;&lt;&quot;Sum: &quot;&lt;&lt;sum/Count;

	console::DontClose();	//Verhindert bei mir das Schließen der Console
}
</code></pre>
<p>Bei betrachten des Tests 2 fällt auf das die &quot;Mitte&quot; des Arrays nur zu 10% getroffen wird, das ist zwar immernoch mehr als Jedes andere einzelne Element, aber zu 90% wird sie eben nicht getroffen. Ich möchte nun das die Mitte häufiger getroffen wird wie bei einer Gaußverteilung, möchte also die &quot;Breite&quot; der Gaußglocke bestimmen können, kann mir da Jemand helfen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/306207/zufallszahl-mit-gewichtung</link><generator>RSS for Node</generator><lastBuildDate>Sat, 08 Aug 2026 08:59:44 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/306207.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 20 Jul 2012 20:32:46 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Zufallszahl mit Gewichtung on Fri, 20 Jul 2012 20:32:46 GMT]]></title><description><![CDATA[<p>Basierten auf meine <a href="http://www.c-plusplus.net/forum/306196" rel="nofollow">Frage</a> hab ich das gebastelt:</p>
<pre><code class="language-cpp">#include &quot;FreeFunktion.h&quot;	//console::DontClose();
#include &lt;iostream&gt;
#include &lt;vector&gt;
using namespace std;

int getRandomNumber(int Number, unsigned Sigma) {
	if(Sigma&lt;2) return Number;
	double P = (double)rand() / RAND_MAX;
	double ItemCount = Sigma*Sigma;
	double ItemsForNextBound = 2;
	double nextBound = ItemsForNextBound/ItemCount;
	int Spread=1;

	while(P &gt;= nextBound) {
		Spread++;
		ItemsForNextBound+=2*(Spread);
		nextBound = ItemsForNextBound/ItemCount;
	}

	if(rand()%2)
		return Number + (Sigma - Spread);
	return Number - (Sigma - Spread);
}

int main() {
	const unsigned Count=1E6;
	double sum=0;
	vector&lt;double&gt; Probs(21);
	cout&lt;&lt;&quot;Test 1: \n&quot;;
	for(unsigned i=0; i&lt;Count; ++i)
		Probs[getRandomNumber(20, 4)-10]++;

	for(unsigned i=0; i&lt;Probs.size(); ++i) {
		cout&lt;&lt;Probs[i]/Count&lt;&lt;&quot;\n&quot;;
		sum+=Probs[i];
	}
	cout&lt;&lt;&quot;Sum: &quot;&lt;&lt;sum/Count;

	cout&lt;&lt;&quot;Test 2: \n&quot;;
	for(unsigned i=0; i&lt;Count; ++i)
		Probs[getRandomNumber(20, 10)-10]++;

	for(unsigned i=0; i&lt;Probs.size(); ++i) {
		cout&lt;&lt;Probs[i]/Count&lt;&lt;&quot;\n&quot;;
		sum+=Probs[i];
	}
	cout&lt;&lt;&quot;Sum: &quot;&lt;&lt;sum/Count;

	console::DontClose();	//Verhindert bei mir das Schließen der Console
}
</code></pre>
<p>Bei betrachten des Tests 2 fällt auf das die &quot;Mitte&quot; des Arrays nur zu 10% getroffen wird, das ist zwar immernoch mehr als Jedes andere einzelne Element, aber zu 90% wird sie eben nicht getroffen. Ich möchte nun das die Mitte häufiger getroffen wird wie bei einer Gaußverteilung, möchte also die &quot;Breite&quot; der Gaußglocke bestimmen können, kann mir da Jemand helfen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2234486</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2234486</guid><dc:creator><![CDATA[Dudelsack]]></dc:creator><pubDate>Fri, 20 Jul 2012 20:32:46 GMT</pubDate></item><item><title><![CDATA[Reply to Zufallszahl mit Gewichtung on Fri, 20 Jul 2012 20:37:38 GMT]]></title><description><![CDATA[<p>Meehhh, mir fällt grad auf das beim kopieren was schief gelaufen ist.<br />
Hier die Richtige Version:</p>
<pre><code class="language-cpp">#include &quot;FreeFunktion.h&quot;	//console::DontClose();
#include &lt;iostream&gt;
#include &lt;vector&gt;
using namespace std;

int getRandomNumber(int Number, unsigned Sigma) {
	if(Sigma&lt;2) return Number;
	double P = (double)rand() / RAND_MAX;
	double ItemCount = Sigma*Sigma;
	double ItemsForNextBound = 2;
	double nextBound = ItemsForNextBound/ItemCount;
	int Spread=1;

	while(P &gt;= nextBound) {
		Spread++;
		ItemsForNextBound+=2*(Spread);
		nextBound = ItemsForNextBound/ItemCount;
	}

	if(rand()%2)
		return Number + (Sigma - Spread);
	return Number - (Sigma - Spread);
}

int main() {
	const unsigned Count=1E6;
	double sum=0;
	vector&lt;double&gt; Probs(21);
	cout&lt;&lt;&quot;Test 1: \n&quot;;
	for(unsigned i=0; i&lt;Count; ++i)
		Probs[getRandomNumber(20, 4)-10]++;

	for(unsigned i=0; i&lt;Probs.size(); ++i) {
		cout&lt;&lt;Probs[i]/Count&lt;&lt;&quot;\n&quot;;
		sum+=Probs[i];
	}
	cout&lt;&lt;&quot;Sum: &quot;&lt;&lt;sum/Count&lt;&lt; &quot;\n&quot;;

	sum=0;
	Probs.clear();
	Probs.resize(21);
	cout&lt;&lt;&quot;Test 2: \n&quot;;
	for(unsigned i=0; i&lt;Count; ++i)
		Probs[getRandomNumber(20, 10)-10]++;

	for(unsigned i=0; i&lt;Probs.size(); ++i) {
		cout&lt;&lt;Probs[i]/Count&lt;&lt;&quot;\n&quot;;
		sum+=Probs[i];
	}
	cout&lt;&lt;&quot;Sum: &quot;&lt;&lt;sum/Count;

	console::DontClose();	//Verhindert bei mir das Schließen der Console
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2234487</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2234487</guid><dc:creator><![CDATA[Dudelsack]]></dc:creator><pubDate>Fri, 20 Jul 2012 20:37:38 GMT</pubDate></item><item><title><![CDATA[Reply to Zufallszahl mit Gewichtung on Fri, 20 Jul 2012 21:48:22 GMT]]></title><description><![CDATA[<p>Die Breite einer Gaußverteilung legt man in der Regel durch sigma fest. Ich bin mir jetzt nicht ganz sicher, was getRandomNumber genau für eine Verteilung erzeugt. Wenn du normalverteilte Zufallszahlen (d.h. Gaußverteilung) erzeugen willst, könnte das hier interessant sein: <a href="http://de.wikipedia.org/wiki/Box-Muller-Methode" rel="nofollow">http://de.wikipedia.org/wiki/Box-Muller-Methode</a>.</p>
<p>Implementiert sieht das dann beispielsweise so aus:</p>
<pre><code class="language-cpp">double useful::rnd_boxMuller(double sigma) {
	double u1 = (rand()+1.) / (RAND_MAX+1.);
	double u2 = (rand()+1.) / (RAND_MAX+1.);
	double z1 = sqrt(-2*log(u1)) * cos(2*M_PI*u2);
	return z1*sigma;
}
</code></pre>
<p>Edit:<br />
Ok, ich hab das Programm einmal ausgeführt. Erzeugt wird so eine dreiecksförmige Verteilung (linearer Anstieg der Wahrscheinlichkeit bis zum Erwartungswert). Willst du diese Form beibehalten? Wenn ja, bleibt dir eigentlich nur die Möglichkeit, ein kleineres Sigma zu wählen. Ansonsten könnte meine Funktion auch interessant sein (die Ausgabe hat den Erwartungswert 0).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2234491</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2234491</guid><dc:creator><![CDATA[Ramanujan]]></dc:creator><pubDate>Fri, 20 Jul 2012 21:48:22 GMT</pubDate></item></channel></rss>