<?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[Zufall wird nicht größer als 327**]]></title><description><![CDATA[<p>Ich beschäftige mich grad mit dem sortieren, und da wollte ich in ein Feld zufällige Werte reinschreiben, die dann anschließend von meiner Funktion &quot;Sort&quot; sortiert werden. So aber irgendwie wird die Zufallszahl nicht größer als 327** und da stört mich irgendwie, da ich eigentlich einen Zufallsrange zwischen 0-50000 wollte (oder später vll. höher).</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;conio.h&gt;
#include &lt;stdlib.h&gt;
using namespace std;

long rnd(long a,long b);
void Swap(int &amp;a,int &amp;b);
void Sort(int *array,int size);

int main()
{
    srand(time(0));
    rand();

    int data[30000];
    for (int i=0;i&lt;30000;i++)
    {
        data[i]=rnd(0,50000); //&lt;-- wird nicht größer als 327**
    }

    int anfang=time(0);

    Sort(data,30000);

    int ende=time(0);
    for (int i=0;i&lt;30000;i++)
    {
        cout &lt;&lt; data[i] &lt;&lt; &quot;\n&quot;;
    }
    cout &lt;&lt; &quot;Es sind &quot; &lt;&lt; ende-anfang &lt;&lt; &quot; Sekunden vergangen.\n&quot;;
    getch();

    return 0;
}

void Sort(int *array,int size)
{
    int minPos;
    for (int feld=0;feld&lt;size-1;feld++)
    {
        minPos=feld;
        for (int i=feld+1;i&lt;size;i++)
        {
            if (array[i]&lt;array[minPos])
            {
                minPos=i;
            }
        }
        Swap(array[feld],array[minPos]);
    }
}

void Swap(int &amp;a,int &amp;b)
{
    int temp=a;
    a=b;
    b=temp;
}

long rnd(long a,long b)
{
     return (rand() % (b-a+1)) + a;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/170945/zufall-wird-nicht-größer-als-327</link><generator>RSS for Node</generator><lastBuildDate>Thu, 17 Sep 2026 08:07:44 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/170945.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 21 Jan 2007 11:56:07 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Zufall wird nicht größer als 327** on Sun, 21 Jan 2007 11:56:07 GMT]]></title><description><![CDATA[<p>Ich beschäftige mich grad mit dem sortieren, und da wollte ich in ein Feld zufällige Werte reinschreiben, die dann anschließend von meiner Funktion &quot;Sort&quot; sortiert werden. So aber irgendwie wird die Zufallszahl nicht größer als 327** und da stört mich irgendwie, da ich eigentlich einen Zufallsrange zwischen 0-50000 wollte (oder später vll. höher).</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;conio.h&gt;
#include &lt;stdlib.h&gt;
using namespace std;

long rnd(long a,long b);
void Swap(int &amp;a,int &amp;b);
void Sort(int *array,int size);

int main()
{
    srand(time(0));
    rand();

    int data[30000];
    for (int i=0;i&lt;30000;i++)
    {
        data[i]=rnd(0,50000); //&lt;-- wird nicht größer als 327**
    }

    int anfang=time(0);

    Sort(data,30000);

    int ende=time(0);
    for (int i=0;i&lt;30000;i++)
    {
        cout &lt;&lt; data[i] &lt;&lt; &quot;\n&quot;;
    }
    cout &lt;&lt; &quot;Es sind &quot; &lt;&lt; ende-anfang &lt;&lt; &quot; Sekunden vergangen.\n&quot;;
    getch();

    return 0;
}

void Sort(int *array,int size)
{
    int minPos;
    for (int feld=0;feld&lt;size-1;feld++)
    {
        minPos=feld;
        for (int i=feld+1;i&lt;size;i++)
        {
            if (array[i]&lt;array[minPos])
            {
                minPos=i;
            }
        }
        Swap(array[feld],array[minPos]);
    }
}

void Swap(int &amp;a,int &amp;b)
{
    int temp=a;
    a=b;
    b=temp;
}

long rnd(long a,long b)
{
     return (rand() % (b-a+1)) + a;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1213607</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1213607</guid><dc:creator><![CDATA[Stromberg]]></dc:creator><pubDate>Sun, 21 Jan 2007 11:56:07 GMT</pubDate></item><item><title><![CDATA[Reply to Zufall wird nicht größer als 327** on Sun, 21 Jan 2007 12:16:35 GMT]]></title><description><![CDATA[<p>Der Wertebereich von int reicht nur bis 2^15-1 also 32767. rand() gibt ein int zurück.</p>
<p>MSDN schrieb:</p>
<blockquote>
<p>The rand function returns a pseudorandom integer in the range 0 to RAND_MAX (32767).</p>
</blockquote>
<p>Edit: Obwohl dein rnd natürlich größere Zahlen zurückgibt...<br />
Auf was du abgesehen davon aufpassen solltest: du weist die long-Werte, die dein rnd zurückgibt, einem int-Feld zu:</p>
<pre><code class="language-cpp">int data[30000];
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1213613</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1213613</guid><dc:creator><![CDATA[Nanuq]]></dc:creator><pubDate>Sun, 21 Jan 2007 12:16:35 GMT</pubDate></item><item><title><![CDATA[Reply to Zufall wird nicht größer als 327** on Sun, 21 Jan 2007 12:20:36 GMT]]></title><description><![CDATA[<p>Wie meinst du das mit größeren Zahlen? Wie schaffe ich es den, das meine &quot;rnd&quot; Funktion größere Zahlen als 32767 zurückgibt?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1213627</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1213627</guid><dc:creator><![CDATA[Stromberg]]></dc:creator><pubDate>Sun, 21 Jan 2007 12:20:36 GMT</pubDate></item><item><title><![CDATA[Reply to Zufall wird nicht größer als 327** on Sun, 21 Jan 2007 12:36:09 GMT]]></title><description><![CDATA[<p>multiplikation</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1213640</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1213640</guid><dc:creator><![CDATA[eine möglichkeit]]></dc:creator><pubDate>Sun, 21 Jan 2007 12:36:09 GMT</pubDate></item><item><title><![CDATA[Reply to Zufall wird nicht größer als 327** on Sun, 21 Jan 2007 12:52:40 GMT]]></title><description><![CDATA[<p>Du verwendest die Funktion rand() und die gibt definitiv maximal 32767 zurück.</p>
<p>Diese Berechnung:</p>
<pre><code class="language-cpp">return (rand() % (b-a+1)) + a;
</code></pre>
<p>ergibt entsprechend für a = 0 und b = 50000 nicht Werte zwischen 0 und 50000 sondern zwischen 0 und 32767.</p>
<p>eine möglichkeit schrieb:</p>
<blockquote>
<p>mulitplikation</p>
</blockquote>
<p>Durch Multiplikation zweier rand()-Ergebnisse kannst du dir natürlich größere Zahlen generieren</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1213641</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1213641</guid><dc:creator><![CDATA[Nanuq]]></dc:creator><pubDate>Sun, 21 Jan 2007 12:52:40 GMT</pubDate></item><item><title><![CDATA[Reply to Zufall wird nicht größer als 327** on Sun, 21 Jan 2007 12:47:57 GMT]]></title><description><![CDATA[<p>die lösung des problems könnte in etwa so sein:</p>
<pre><code class="language-cpp">long rnd(long a,long b)
{
    int SQRandMax=std::sqrt(b-a+1);

    return (rand() % SQRandMax)*(rand() % SQRandMax) + a;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1213650</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1213650</guid><dc:creator><![CDATA[otze]]></dc:creator><pubDate>Sun, 21 Jan 2007 12:47:57 GMT</pubDate></item><item><title><![CDATA[Reply to Zufall wird nicht größer als 327** on Sun, 21 Jan 2007 13:44:39 GMT]]></title><description><![CDATA[<p>otze schrieb:</p>
<blockquote>
<p>die lösung des problems könnte in etwa so sein:</p>
<pre><code class="language-cpp">long rnd(long a,long b)
{
    int SQRandMax=std::sqrt(b-a+1);

    return (rand() % SQRandMax)*(rand() % SQRandMax) + a;
}
</code></pre>
</blockquote>
<p>Das liefert allerdings keine Gleichverteilung. Wie man sofort sieht, liefert das Produkt schon mal keine Primzahlen zwischen SQRandMax und (b-a+1). Mein Vorschlag wäre</p>
<pre><code class="language-cpp">long rnd(long a,long b)
{
    return (rand()*(RAND_MAX+1)+rand()) % (b-a+1) + a;
}
</code></pre>
<p>[</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1213725</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1213725</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Sun, 21 Jan 2007 13:44:39 GMT</pubDate></item><item><title><![CDATA[Reply to Zufall wird nicht größer als 327** on Sun, 21 Jan 2007 13:55:33 GMT]]></title><description><![CDATA[<p>Multiplikation ist echt ne ... Lösung:</p>
<pre><code class="language-cpp">long rnd()
{
    unsigned long l = rand();
    unsigned long l2 = rand();
    return l &lt;&lt; (sizeof(int) / 8) + l2;
}
</code></pre>
<p>oder so ähnlich...</p>
<p>Durch Bit-shifting halt...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1213734</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1213734</guid><dc:creator><![CDATA[Shifter]]></dc:creator><pubDate>Sun, 21 Jan 2007 13:55:33 GMT</pubDate></item><item><title><![CDATA[Reply to Zufall wird nicht größer als 327** on Sun, 21 Jan 2007 16:10:30 GMT]]></title><description><![CDATA[<p>Shifter schrieb:</p>
<blockquote>
<p>Multiplikation ist echt ne ... Lösung:</p>
<pre><code class="language-cpp">long rnd()
{
    unsigned long l = rand();
    unsigned long l2 = rand();
    return l &lt;&lt; (sizeof(int) / 8) + l2;
}
</code></pre>
<p>oder so ähnlich...</p>
<p>Durch Bit-shifting halt...</p>
</blockquote>
<p>dann verlierst du die 0 und 1</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1213827</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1213827</guid><dc:creator><![CDATA[Stelfer]]></dc:creator><pubDate>Sun, 21 Jan 2007 16:10:30 GMT</pubDate></item><item><title><![CDATA[Reply to Zufall wird nicht größer als 327** on Sun, 21 Jan 2007 17:30:40 GMT]]></title><description><![CDATA[<p>Alle zahlen bis 50000 wird er eh nie durch multiplikation erreichen können. Bei Primzahlen wäre das unmöglich.</p>
<p>Addition wäre da besser zwei rand() funktionen die von 0-25000 gehen wären eine bessere Lösung.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1213899</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1213899</guid><dc:creator><![CDATA[walljumper]]></dc:creator><pubDate>Sun, 21 Jan 2007 17:30:40 GMT</pubDate></item><item><title><![CDATA[Reply to Zufall wird nicht größer als 327** on Sun, 21 Jan 2007 23:56:38 GMT]]></title><description><![CDATA[<p>Das einfachste wäre wohl einen Generator zu verwenden der einen definierten Wertebereich ausspuckt, also z.B. fix 0 - (2^32-1) oder sowas. z.B. den boost::mt19937 aus Boost.Random.</p>
<p>Alles andere wird schnell ein wenig kompliziert wenn das Programm für beliebige Werte von RAND_MAX funktionieren soll.</p>
<p>Der Vorschlag von camper ist gut solange 0 - (RAND_MAX+1)^2-1 als Wertebereich ausreichend ist, und RAND_MAX klein genug ist dass es dabei nicht zu einem Überlauf kommt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1214133</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1214133</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Sun, 21 Jan 2007 23:56:38 GMT</pubDate></item><item><title><![CDATA[Reply to Zufall wird nicht größer als 327** on Mon, 22 Jan 2007 00:20:30 GMT]]></title><description><![CDATA[<p>Guckt Ihr:</p>
<p><a href="http://www.c-plusplus.net/forum/viewtopic.php?t=39343" rel="nofollow">http://www.c-plusplus.net/forum/viewtopic.php?t=39343</a><br />
<a href="http://www.c-plusplus.net/forum/viewtopic.php?t=39344" rel="nofollow">http://www.c-plusplus.net/forum/viewtopic.php?t=39344</a><br />
<a href="http://www.c-plusplus.net/forum/viewtopic.php?t=39331" rel="nofollow">http://www.c-plusplus.net/forum/viewtopic.php?t=39331</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1214142</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1214142</guid><dc:creator><![CDATA[CodeFinder]]></dc:creator><pubDate>Mon, 22 Jan 2007 00:20:30 GMT</pubDate></item><item><title><![CDATA[Reply to Zufall wird nicht größer als 327** on Mon, 22 Jan 2007 09:42:52 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/12048">@CodeFinder</a>:</p>
<p>und wo genau steht dort irgendetwas hilfreiches zur Frage wie man den Bereich der Zufallszahlen erweitern kann?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1214284</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1214284</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Mon, 22 Jan 2007 09:42:52 GMT</pubDate></item></channel></rss>