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.

[Turbo C++]randomize "nur" zwischen zwei Zahlen

Empfohlene Antworten

Veröffentlicht

Hi,

also ich will eine Zufallszahl erzeugen.

Jedoch darf diese Zufallszahl entweder "2" oder "-2" sein.

ich hab' das jetzt soweit:

TurboC++


#include <stdio.h>

#include <stdlib.h>


void main()

{

int zufall=0;

randomize ();


zufall= random ([B]?[/B])+[B]?[/B];


printf ("%i", zufall);

}

was muss ich jetzt bei den Fragezeichen hinschreiben, damit

er nur zwischen den Zahlen "-2" und "2" auswählt?

Vielen Dank

Mit freundlichen Grüßen

looop

Du solltest anstatt random, srandom verwenden und wenn ich das noch richtig im Kopf habe erzeugen beide eine Zahl im Intervall [0,1]. Damit musst Du nun den Zahlenbereich auf das Intervall [-2,2] abbilden. Somit ergibt sich dann -2 + rand() * 4.

Denke aber daran, Du musst den Zufallsgenerator auch initialisieren, sonst erhälst Du immer die gleiche Zahlenfolge

Hoffe ich hab mich nicht vertan

Phil


int rndm = rand()%2 ? -2 : 2;
[/php]

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

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.