<?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 ohne Doppelwerte]]></title><description><![CDATA[<p>Hallo.</p>
<p>Ich möchte in ein Feld von MAX_PIXELS Elementen die Elemente 1 bis MAX_PIXELS zufällig hineinschreiben, ohne dass ein Wert doppelt vorkommt.</p>
<p>Meine Lösung sieht so aus:</p>
<pre><code class="language-cpp">void sort::CreateRandomPixels()
{
	int i, j;
	for(i=1;i&lt;MAX_PIXELS;i++) pixels[i]=-1;

	for(i=1;i&lt;MAX_PIXELS;i++)
	{
		j = rand()%MAX_PIXELS;
		while(pixels[j%MAX_PIXELS]&gt;=0)
		{
			j++;
		}

		pixels[j%MAX_PIXELS]=i;
	};
};
</code></pre>
<p>Das ganze funktioniert zwar und es kommt auch kein Wert doppelt vor, allerdings wenn ich das zeichnen lasse sieht das nicht wirklich so aus, als ob es gleichverteilt ist. Hat jemand eine andere Möglichkeit, wie man sowas machen kann? Diese while-Schleife ist meines Erachtens das Problem.</p>
<p>Grüße<br />
Max</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/194059/zufallszahlen-ohne-doppelwerte</link><generator>RSS for Node</generator><lastBuildDate>Mon, 28 Sep 2026 08:35:56 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/194059.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 03 Oct 2007 12:24:11 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Zufallszahlen ohne Doppelwerte on Wed, 03 Oct 2007 12:24:11 GMT]]></title><description><![CDATA[<p>Hallo.</p>
<p>Ich möchte in ein Feld von MAX_PIXELS Elementen die Elemente 1 bis MAX_PIXELS zufällig hineinschreiben, ohne dass ein Wert doppelt vorkommt.</p>
<p>Meine Lösung sieht so aus:</p>
<pre><code class="language-cpp">void sort::CreateRandomPixels()
{
	int i, j;
	for(i=1;i&lt;MAX_PIXELS;i++) pixels[i]=-1;

	for(i=1;i&lt;MAX_PIXELS;i++)
	{
		j = rand()%MAX_PIXELS;
		while(pixels[j%MAX_PIXELS]&gt;=0)
		{
			j++;
		}

		pixels[j%MAX_PIXELS]=i;
	};
};
</code></pre>
<p>Das ganze funktioniert zwar und es kommt auch kein Wert doppelt vor, allerdings wenn ich das zeichnen lasse sieht das nicht wirklich so aus, als ob es gleichverteilt ist. Hat jemand eine andere Möglichkeit, wie man sowas machen kann? Diese while-Schleife ist meines Erachtens das Problem.</p>
<p>Grüße<br />
Max</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1377087</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1377087</guid><dc:creator><![CDATA[Max3000]]></dc:creator><pubDate>Wed, 03 Oct 2007 12:24:11 GMT</pubDate></item><item><title><![CDATA[Reply to Zufallszahlen ohne Doppelwerte on Wed, 03 Oct 2007 12:48:51 GMT]]></title><description><![CDATA[<p>seh jetzt nich richtig durch bei deinem algorithmus. aber nimm doch std::random_shuffle:</p>
<pre><code class="language-cpp">#include &lt;algorithm&gt;
int ar[num];
for(int i = 0; i &lt; num; i++) ar[i] = i;
std::random_shuffle(ar, ar+num);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1377101</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1377101</guid><dc:creator><![CDATA[Maxi]]></dc:creator><pubDate>Wed, 03 Oct 2007 12:48:51 GMT</pubDate></item></channel></rss>