<?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[Probleme mit Zufallszahlen]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich habe ein Problem bei der Generierung von Zufallszahlen. Ich bekomme da immer die gleichen raus.</p>
<p>hier mal mein code, wie ich die zahlen erzeuge:</p>
<pre><code>int VerTyp= 0;
    char typ;
    srand(( unsigned )time( NULL ));      // Startpunkt setzen
    VerTyp= rand() % 2;             // Zufallszahlen von 0 - 1 erzeugen
    if(VerTyp == 0)
	typ= 'A';
    if(VerTyp == 1)
	typ= 'B';
</code></pre>
<p>Wenn ich das z.B. als Schleife laufen lasse, bekomme ich immer wieder die fünf gleichen Zahlen, bzw. hier würde ich dann ja rausbekommen, dass typ z.B immer gleich A ist.</p>
<p>kann mir da jemand weiterhelfen, dass ich da verschiede Zahlen raus bekomme.</p>
<p>Vielen Dank<br />
gruß<br />
Foxy</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/127490/probleme-mit-zufallszahlen</link><generator>RSS for Node</generator><lastBuildDate>Tue, 25 Aug 2026 03:18:38 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/127490.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 24 Nov 2005 17:31:15 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Probleme mit Zufallszahlen on Thu, 24 Nov 2005 17:31:15 GMT]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich habe ein Problem bei der Generierung von Zufallszahlen. Ich bekomme da immer die gleichen raus.</p>
<p>hier mal mein code, wie ich die zahlen erzeuge:</p>
<pre><code>int VerTyp= 0;
    char typ;
    srand(( unsigned )time( NULL ));      // Startpunkt setzen
    VerTyp= rand() % 2;             // Zufallszahlen von 0 - 1 erzeugen
    if(VerTyp == 0)
	typ= 'A';
    if(VerTyp == 1)
	typ= 'B';
</code></pre>
<p>Wenn ich das z.B. als Schleife laufen lasse, bekomme ich immer wieder die fünf gleichen Zahlen, bzw. hier würde ich dann ja rausbekommen, dass typ z.B immer gleich A ist.</p>
<p>kann mir da jemand weiterhelfen, dass ich da verschiede Zahlen raus bekomme.</p>
<p>Vielen Dank<br />
gruß<br />
Foxy</p>
]]></description><link>https://www.c-plusplus.net/forum/post/926315</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/926315</guid><dc:creator><![CDATA[Foxy]]></dc:creator><pubDate>Thu, 24 Nov 2005 17:31:15 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit Zufallszahlen on Thu, 24 Nov 2005 17:37:36 GMT]]></title><description><![CDATA[<p>Rufe srand() nur einmal vor der Schleife aufrufen.</p>
<p>Gruß</p>
]]></description><link>https://www.c-plusplus.net/forum/post/926319</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/926319</guid><dc:creator><![CDATA[FireFlow]]></dc:creator><pubDate>Thu, 24 Nov 2005 17:37:36 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit Zufallszahlen on Thu, 24 Nov 2005 17:48:58 GMT]]></title><description><![CDATA[<p>dann bekomme ich für den ersten z.B. A und für die weiteren 4 B bei 5 Schleifen durchläufen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/926334</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/926334</guid><dc:creator><![CDATA[Foxy]]></dc:creator><pubDate>Thu, 24 Nov 2005 17:48:58 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit Zufallszahlen on Thu, 24 Nov 2005 17:53:47 GMT]]></title><description><![CDATA[<p>Und? Wenn Du abwechselnd A und B haben willst, dann implementier es so. Wenn Du was zufälliges haben willst, dann wunder Dich auch nicht, daß zufällig einmal A und dann 4mal B rauskommt.</p>
<p>Oder passiert das bei jedem Programmdurchlauf? Dann solltest Du mal Deinen kompletten Code zeigen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/926339</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/926339</guid><dc:creator><![CDATA[Jester]]></dc:creator><pubDate>Thu, 24 Nov 2005 17:53:47 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit Zufallszahlen on Thu, 24 Nov 2005 18:04:26 GMT]]></title><description><![CDATA[<p>bei einem weiteren durchlauf hate ich 5mal b und bei einem dritten 5 mal a undbei einem 4. wieder 5 mal b:<br />
klar, kann es sein, dass es mal vorkommt, dass alles a oder b ist, aber das kommt ja immer vor.</p>
<p>hier mal mein code:<br />
so wird die funktion random() aufgerufen:</p>
<pre><code>.....
int Anzahl= 5;
for(ii= 0; ii &lt; Anzahl; ii++) 
{
    typ= random(ii);
    .....
}
</code></pre>
<pre><code>char random(int ii)
{
    int VerTyp= 0;
    char typ;
    if(ii == 0)
        srand(( unsigned )time( NULL ));       
    VerTyp= rand() % 2;             
    if(VerTyp == 0)
	typ= 'A';
    if(VerTyp == 1)
	typ= 'B';
    std::ofstream file;
    file.open(&quot;typ.txt&quot;,std::ios_base::app);
    if ( !file )
    {
        TRACE( &quot;Can't open file&quot; ); /ausgegeben
    }
    file &lt;&lt; &quot;Typ: &quot; &lt;&lt; typ &lt;&lt; '\n';
    return typ;
    file.close();

}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/926350</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/926350</guid><dc:creator><![CDATA[Foxy]]></dc:creator><pubDate>Thu, 24 Nov 2005 18:04:26 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit Zufallszahlen on Thu, 24 Nov 2005 18:13:02 GMT]]></title><description><![CDATA[<p>Vielleicht so:</p>
<pre><code class="language-cpp">typ = ((rand()/RAND_MAX) &lt; 0.5)?'A':'B';
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/926357</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/926357</guid><dc:creator><![CDATA[.filmor]]></dc:creator><pubDate>Thu, 24 Nov 2005 18:13:02 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit Zufallszahlen on Thu, 24 Nov 2005 18:18:47 GMT]]></title><description><![CDATA[<p>da kommt bei drei durchläufen immer A heraus</p>
]]></description><link>https://www.c-plusplus.net/forum/post/926362</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/926362</guid><dc:creator><![CDATA[Foxy]]></dc:creator><pubDate>Thu, 24 Nov 2005 18:18:47 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit Zufallszahlen on Thu, 24 Nov 2005 18:22:41 GMT]]></title><description><![CDATA[<p>Dann nimm halt <a href="http://www.boost.org/libs/random" rel="nofollow">Boost.Random</a>, das hat auch einen Zufallsgenerator für zwei Werte. Wenn du halbwegs &quot;echte&quot; Zufallszahlen haben willst ist der lineare Kongruenzkrams aus der cstdlib eh nicht zu gebrauchen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/926366</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/926366</guid><dc:creator><![CDATA[.filmor]]></dc:creator><pubDate>Thu, 24 Nov 2005 18:22:41 GMT</pubDate></item><item><title><![CDATA[Reply to Probleme mit Zufallszahlen on Fri, 25 Nov 2005 07:31:11 GMT]]></title><description><![CDATA[<p>.filmor schrieb:</p>
<blockquote>
<p>Vielleicht so:</p>
<pre><code class="language-cpp">typ = ((rand()/RAND_MAX) &lt; 0.5)?'A':'B';
</code></pre>
</blockquote>
<p>Das kann nichts werden wegen Ganzzahl-Arithmetik - d.h. du müsstest entweder rand() oder RAND_MAX nach double casten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/926643</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/926643</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Fri, 25 Nov 2005 07:31:11 GMT</pubDate></item></channel></rss>