Archiv verlassen und diese Seite im Standarddesign anzeigen : Array mit unterschiedlichen Zufallszahlen füllen
outlaw_wolf
2005-01-27, 16:06:41
Ich möchte ein Array (lottozahlen) mit unterschiedlichen Zufallszahlen von 1-49 füllen.
Die bedingung ist, dass keine Zahl doppelt sein darf.
Allerdings stimmt mein Programm nicht.
Ich habe immer zahlen doppelt....
#include<iostream>
#include<ctime>
using namespace std;
void quicksort (int [], int, int);
int main()
{
srand( time(0));
const int size = 20; // zum testen
int lottozahlen[size];
lottozahlen[0]=rand() %49+1;
for(int i=1; i<size; i++)
for(int j=i-1; j>=0; j--)
if(lottozahlen[i] != lottozahlen[j])
lottozahlen[i]=rand() %49+1;
// ausgabe1
cout << "unsort" << endl;
for(int ausgabe=0; ausgabe<size; ausgabe++)
cout << lottozahlen[ausgabe] << endl;
quicksort(lottozahlen, 0, size);
//ausgabe2
cout << "sort" << endl;
for(ausgabe=0; ausgabe<size; ausgabe++)
cout << lottozahlen[ausgabe] << endl;
return 0;
}
void quicksort(int a[], int l, int r){
if(r>l){
int i=l-1, j=r, tmp;
for(;;)
{
while(a[++i]<a[r]);
while(a[--j]>a[r]);
if(i>=j) break;
tmp=a[i]; a[i]=a[j]; a[j]=tmp;
}
tmp=a[i]; a[i]=a[r]; a[r]=tmp;
quicksort(a, l, i-1);
quicksort(a, i+1, r);
}
}
Sephiroth
2005-01-27, 16:35:55
Da sagst du zwar, was passieren soll, wenn sie verschieden sind aber wenn sie gleich sind, sollte doch auch noch was passieren - nicht? ;) Wenn sie gleich sind, dann läßt er die Zahlen doch so.
p.s.
ich hab noch nen ultra primitives Lottozahlen TPW Prog ohne doppelte Zahlen X-D
outlaw_wolf
2005-01-27, 16:41:25
Jo, aber wie mache ich das?
Sephiroth
2005-01-27, 16:47:07
Jo, aber wie mache ich das?
das
lottozahlen[0]=rand() %49+1;
for(int i=1; i<size; i++)
for(int j=i-1; j>=0; j--)
if(lottozahlen[i] != lottozahlen[j])
lottozahlen[i]=rand() %49+1;
durch
for(int k=0; k<size; k++)
lottozahlen[k]=rand() %49+1;
for(int i=0; i<size; i++)
for(int j=i+1; j<size; j++)
if(lottozahlen[i] == lottozahlen[j])
lottozahlen[j]=rand() %49+1;
ersetzen?
outlaw_wolf
2005-01-27, 16:47:27
Kann es sei, dass man das nur in einer rekursiven Funktion lösen kann? :confused:
Sephiroth
2005-01-27, 16:51:29
Was mir grad noch einfällt ...
/edit:
*mumpitz ist das, da hab ich wohl nicht nachgedacht*
outlaw_wolf
2005-01-27, 17:54:49
Das Prgramm stellt nicht zu 100% sicher das es unterschiedliche Zahlen sind.
In der 2. Schleife könnten wieder die selben Zahlen gespeichert werden.
Gnafoo
2005-01-27, 18:17:00
Wenn der STL-Vector auch ok ist, hätte ich noch ne alternative Lösung, bei der man keinen eigenen Sort-Algorithmus braucht:
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;
int main()
{
srand(time(0));
const int size = 20; // zum testen
vector<int> lottozahlen;
while(lottozahlen.size() != 20)
{
int nextnum = rand() %49+1;
if(find(lottozahlen.begin(), lottozahlen.end(), nextnum) != lottozahlen.end())
continue;
lottozahlen.push_back(nextnum);
}
sort(lottozahlen.begin(), lottozahlen.end());
for(int i=0; i<lottozahlen.size(); i++)
cout << lottozahlen[i] << endl;
}
cu DerTod
outlaw_wolf
2005-01-27, 18:49:44
Ne, ist leider nicht ok, wir sollen mit dynamischen Array arbeiten...
Hier mal die gesammte Aufgabenstellung:
http://www.fbi.fh-darmstadt.de/%7Eh.p.weber/Lehrveranstaltungen/PG1_Praktikum/Lab6/PG1Klaus.htm
Ich bin überhaupt erst mal am testen wie ich die Lottezahlen überhaupt erstellen kann.
outlaw_wolf
2005-01-27, 19:28:42
Mittlerweile bin ihc mit viel Hilfe auf diesen Code gekommen:
#include<iostream>
#include<ctime>
using namespace std;
void quicksort (int [], int, int);
int main()
{
srand( time(0));
const int si = 49; // zum testen
int lottozahlen[si];
bool doppelt = false;
for(int i=0; i<si; i++)
{
lottozahlen[i]=rand() %49+1;
if (i>0)
{
do
{
doppelt = false;
for(int j=i-1; j>=0; j--)
{
if(lottozahlen[i] == lottozahlen[j])
{
lottozahlen[i]=rand() %49+1;
doppelt = true;
break;
}
}
}while (doppelt==true );
}
}
// ausgabe1
cout << "unsort" << endl;
for(int ausgabe=0; ausgabe<si; ausgabe++)
cout << ausgabe+1 << ": " << lottozahlen[ausgabe] << endl;
quicksort(lottozahlen, 0, si);
//ausgabe2
cout << "sort" << endl;
for(ausgabe=0; ausgabe<si; ausgabe++)
cout << ausgabe+1 << ": " << lottozahlen[ausgabe] << endl;
return 0;
}
void quicksort(int a[], int l, int r){
if(r>l){
int i=l-1, j=r, tmp;
for(;;)
{
while(a[++i]<a[r]);
while(a[--j]>a[r]);
if(i>=j) break;
tmp=a[i]; a[i]=a[j]; a[j]=tmp;
}
tmp=a[i]; a[i]=a[r]; a[r]=tmp;
quicksort(a, l, i-1);
quicksort(a, i+1, r);
}
}
Damit läuft es...
ScottManDeath
2005-01-27, 19:40:10
vector <int> lotto(49);
for (int i = 1 ; i < 49 ; i++ )
lotto[i] = i + 1;
random_shuffle(lotto.begin( ), lotto.end( ));
Dann stehen im vector lotto alle Zahlen genau einmal drinne, in zufälliger Reihenfolge. Zugriff auf vektor wie ein normales array.
Vedek Bareil
2005-01-28, 19:18:23
vector<int> RemnantBullet(49); // noch in Lottotrommel vorhandene Kugeln
for (int i=0; i < 49; i++) RemnantBullet[i] = i+1; // mit Zahlen 1 - 49 beschriften
for (int loop = 0; loop < 6; loop++)
{
// eine der noch vorhandenen Kugeln ziehen:
int iBullet = rand() / (RemnantBullet.size() + 1);
// gezogene Kugel aus Menge der noch vorhandenen Kugeln entfernen:
RemnantBullet.erase(RemnantBullet.begin() + iBullet);
}
fertig :)
HellHorse
2005-01-28, 19:42:30
SCNR
numbers = (1...49).sort_by {rand}
for index in 0..@@ARRAY_SIZE
@sixNumbers[index] = numbers.pop
end
Vedek Bareil
2005-01-28, 20:33:54
@HellHorse: was ist denn das für eine Programmiersprache? Perl? PHP?
Und was genau macht dein Code?
HellHorse
2005-01-28, 22:20:21
Ruby (http://www.ruby-lang.org/en/)
(1...49)
generiert eine range von 1 bis und mit 49
sort_by {rand}
Mischt die range (sortiert mit Zufallsfunktion als Vergleichsfuntion) und macht ein Array daraus.
0..@@ARRAY_SIZE
genertiert eine neue range von 0 bis ARRAY_SIZE - 1.
for index in
iteriert über jedes Element in der range. Das sind alle gültigen Array Indizes.
äquivalent zu
for (int index = 0; index < ARRAY_SIZE; ++index) {
}
@sixNumbers[index] = numbers.pop
Das oberste Element aus dem gemischten Array mit Zahlen wird entfernt in das Array sixNumbers (Instanzvariable) geschrieben am aktuellen Index.
Zusammengefasst:
Füllt ein Array der Länge ARRAY_SIZE mit zufälligen Zahlen von 1 bis 49. Keine Zahl kommt doppelt vor.
Frag nicht wie lange 1'000'000 Ziehungen dauern ;)
ethrandil
2005-01-29, 03:47:45
TreeSet ziehung = new TreeSet();
for(int i = anzahl; i-->0; ){
boolean erfolgreich = false;
while(!erfolgreich){
Integer nr = new Integer((int)(Math.random()*48+1));
if(!ziehung.contains(nr)){
ziehung.add(nr);
erfolgreich = true;
}
}
}
:|
- Eth
vBulletin®, Copyright ©2000-2025, Jelsoft Enterprises Ltd.