Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Zufallszahlen ausgeben

Empfohlene Antworten

Veröffentlicht

Hallo @ alle. Ich bin neu hier und habe da eine frage. Ich habe bereits die SuFu benutzt und habe nichts gefunden :( . Ich wollte fragen wie ich in einer c++ konsolen anwendung 4 zufallszahlen zwischen 0 und 53 ausgeben kann? Weil ich möchte gerne ein Karten spiel programmieren und desshalb sollten die karten nicht immer die selbe sein. Danke für Antworten

MfG errox

Morgen,

Zufallszahlen erzeugt man mit der parameterlosen Funktion rand(),

die durch stdlib.h zur Verfügung steht.

Eine Zufallszahl zwischen a und b (incl. a und B) erzeugt man z.B. mit:

a + ( rand() % ( b - a + 1 ) )

d.h. 1 bis 100 erzeugt man mit:

1 + ( rand() % ( 100 - 1 + 1 ) )

also

1 + ( rand() % 100 )

Da rand() immer gleiche Folgen von Zufallszahlen - also

Pseudozufallszahlen - liefert,

benötigt man noch einen "Start" für rand():

Den "echten" Zufallseffekt erzeugt man, indem man rand() zunächst mittels:

srand( (unsigned) time(NULL) ) ;

oder

time(&sec);

srand( (unsigned) sec);

initialisiert. Dafür benötigst Du noch time.h im Programmkopf.

  • Autor

VIELEN VIELEN DANK!!!!

kann ich auch irgentwie das in eine variable speichern Das sie sich auch noch verändert nicht immer gleich bleibt? siehe code

#include "stdafx.h"

#include "iostream"

#include "windows.h"

#include "time.h"

#include "stdlib.h"

#include "string"

using namespace std;

int main()

{



	srand( (unsigned) time(NULL) );

	int random = 1 + ( rand() % 49 );

	while(1)

	{


		cout << random;

		Sleep (100);

	}



	system("PAUSE");

	return EXIT_SUCCESS;

}

wäre schön wenn sich die variable verändern würde...

danke für antworten

Wie wäre es, wenn du die Zufallszahl mal in deiner Schleife änderst?

In deinem Programm die funktion rand genau einmal ausgeführt.

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.