Autor Beitrag
luckyluc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 103



BeitragVerfasst: So 15.02.09 17:14 
Hallo
im folgenden quelltext möchte ich das das nur die hälfte von steabe als zufallszahl gesucht wird, doch wenn ich
random (staebe/2); schreibe zeigt er mir incompatibles types an. wie kann ich das problem lösen?
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
procedure TForm1.Button1Click(Sender: TObject);
begin
randomize;
zug := random (staebe);
Label2.Caption := IntToStr (zug);
staebe:= (staebe - zug);
Label1.Caption := IntToStr (staebe);

end;

DANKE!
jakobwenzel
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1889
Erhaltene Danke: 1

XP home, ubuntu
BDS 2006 Prof
BeitragVerfasst: So 15.02.09 17:17 
Random erwartet eine Ganzzahl als Parameter - beim Teilen kommen aber Kommazahlen raus.

Lösung entweder runden (round) oder gleich Ganzzahldivision mit div benutzen.

_________________
I thought what I'd do was, I'd pretend I was one of those deaf-mutes.
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: So 15.02.09 17:17 
Nimm Round oder Div, um bei der Division durch zwei wieder einen Integer zu bekommen, wie ihn Random() erwartet.

Edit: Das war wohl fast zeitgleich gepostet - ich hab nix orangenes gesehn. :lupe:

_________________
We are, we were and will not be.
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: So 15.02.09 17:20 
Oder du shiftest die Bits einmal nach rechts, was einer Division durch 2 entspricht:
ausblenden Delphi-Quelltext
1:
random(staebe shr 1);					


Edit: Randomize für gleichmäßig verteilte Zufallszahlen besser ins FormCreate. ;)
luckyluc Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 103



BeitragVerfasst: So 15.02.09 17:28 
ok. dankeschön :D .
weiß jemand wie ich nun verhindern kann, dass sich randomize zufällig die zahl 0 aussucht?
JayEff
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2971

Windows Vista Ultimate
D7 Enterprise
BeitragVerfasst: So 15.02.09 17:30 
user profile iconluckyluc hat folgendes geschrieben Zum zitierten Posting springen:
weiß jemand wie ich nun verhindern kann, dass sich randomize zufällig die zahl 0 aussucht?
Neue Frage neues topic, aber lass random einfach solange laufen, bis die Zahl keine 0 ist:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
zahl := 0;
while zahl = 0 do
  Zahl := random(range);
//zahl ist ne zufallszahl die nicht 0 ist

Edit: vorsicht endlosschleife, wenn range=0 :rofl:

_________________
>+++[>+++[>++++++++<-]<-]<++++[>++++[>>>+++++++<<<-]<-]<<++
[>++[>++[>>++++<<-]<-]<-]>>>>>++++++++++++++++++.+++++++.>++.-.<<.>>--.<+++++..<+.


Zuletzt bearbeitet von JayEff am So 15.02.09 17:32, insgesamt 1-mal bearbeitet
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: So 15.02.09 17:31 
user profile iconJayEff hat folgendes geschrieben Zum zitierten Posting springen:
random einfach solange laufen, bis die Zahl keine 0 ist:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
zahl := 0;
while zahl = 0 do
  Zahl := random(range);
//zahl ist ne zufallszahl die nicht 0 ist

Da random nur positiv natürliche Zahlen erzeugt, doch einfach: :gruebel:
ausblenden Delphi-Quelltext
1:
random( bereich ) + 1;					
JayEff
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 2971

Windows Vista Ultimate
D7 Enterprise
BeitragVerfasst: So 15.02.09 17:34 
user profile iconMarc. hat folgendes geschrieben Zum zitierten Posting springen:
ausblenden Delphi-Quelltext
1:
random( bereich ) + 1;					
:oops: das ist natürlich offensichtlich sauberer. Aber meines könnte man auf jede beliebige Zahl verallgemeinern :)

_________________
>+++[>+++[>++++++++<-]<-]<++++[>++++[>>>+++++++<<<-]<-]<<++
[>++[>++[>>++++<<-]<-]<-]>>>>>++++++++++++++++++.+++++++.>++.-.<<.>>--.<+++++..<+.
luckyluc Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 103



BeitragVerfasst: So 15.02.09 17:39 
achso ja danke!
Hybrid666
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 26



BeitragVerfasst: So 15.02.09 17:47 
einfach ma zum verständniss:

du willst eine zufallszahl, die von a bis b geht dann machst du:

ausblenden Delphi-Quelltext
1:
zufall := random (b - a + 1) + a;					


Damit kannste dir nun nen bereich definieren der einfach keine 0 enthällt ;) mit ein bisschen überlegen kommst du auch drauf warum grade diese "formel".

MfG
jakobwenzel
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1889
Erhaltene Danke: 1

XP home, ubuntu
BDS 2006 Prof
BeitragVerfasst: So 15.02.09 17:57 
Oder man nimmt RandomRange aus der Unit Math:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
function RandomRange(const AFrom, ATo: Integer): Integer;
begin
  if AFrom > ATo then
    Result := Random(AFrom - ATo) + ATo
  else
    Result := Random(ATo - AFrom) + AFrom;
end;

_________________
I thought what I'd do was, I'd pretend I was one of those deaf-mutes.
ub60
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 765
Erhaltene Danke: 130



BeitragVerfasst: So 15.02.09 19:18 
user profile iconjakobwenzel hat folgendes geschrieben Zum zitierten Posting springen:
Oder man nimmt RandomRange aus der Unit Math:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
function RandomRange(const AFrom, ATo: Integer): Integer;
begin
  if AFrom > ATo then
    Result := Random(AFrom - ATo) + ATo
  else
    Result := Random(ATo - AFrom) + AFrom;
end;


Hab ich mir doch gleich gedacht: Die Implementierung in der Unit Math ist FALSCH!
Wer es nicht glaubt, probiere dieses:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
uses Math;

procedure TForm1.Button1Click(Sender: TObject);
var RArray : array[1..10] of Integer;
    i : Integer;
begin
  for i:=1 to 10 do RArray[i]:=0;
  for i:=1 to 10000 do
    Inc(RArray[RandomRange(1,10)]);
  for i:=1 to 10 do
    ListBox1.Items.Add(IntToStr(i)+':'+IntToStr(RArray[i]));
end;

Bei der Zahl 10 erscheint kein Wert, obwohl dort etwa die gleiche Anzahl wie bei den anderen erscheinen sollte.

ub60
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 15.02.09 19:35 
Es ist genauso programmiert wie Random selbst. :nixweiss:

Random() gibt einen Fließkommawert 0 <= Wert < 1.0 zurück.
Random(x) gibt einen Integerwert 0 <= Wert < x zurück.
RandomRange(x, y) gibt einen Integerwert x <= Wert < y zurück.

Wenn du das als falsch ansiehst...

// EDIT:
Siehe Hilfe:
Hilfe hat folgendes geschrieben:
RandomRange returns a random integer from the range that extends between AFrom and ATo (non-inclusive). RandomRange can handle negative ranges (where AFrom is greater than ATo).

// EDIT2:
Ok, bei Delphi 7 ist sie falsch, wie so vieles in der alten Hilfe. Da steht inklusive dahinter...
Ab Turbo Delphi stimmts aber, da ist die Hilfe ja eh übersichtlicher.
ub60
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 765
Erhaltene Danke: 130



BeitragVerfasst: So 15.02.09 21:00 
user profile iconjaenicke hat folgendes geschrieben Zum zitierten Posting springen:

// EDIT2:
Ok, bei Delphi 7 ist sie falsch, wie so vieles in der alten Hilfe. Da steht inklusive dahinter...
Ab Turbo Delphi stimmts aber, da ist die Hilfe ja eh übersichtlicher.


OK, ich hatte in der D7-Hilfe nachgeschaut :wink:
Allerdings ist so vom normalen Menschenverstand her der BIS-Wert immer eingeschlossen, auch wenn es anders in der Hilfe steht :twisted:

ub60
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 15.02.09 21:21 
user profile iconub60 hat folgendes geschrieben Zum zitierten Posting springen:
OK, ich hatte in der D7-Hilfe nachgeschaut :wink:
Davon rate ich ab. ;-)
Das ist nicht der einzige Fehler. Außerdem ist sie relativ unübersichtlich.
Der einzige, wirklich der einzige, Vorteil der D7-Hilfe war, dass auch Beispiele dabei waren. Da die aber auch oft falsch oder zumindest unsauber geschrieben waren...

Nimm lieber die Online-Hilfe für D2007 z.B. oder die von D2009, die man herunterladen kann. Oder natürlich die von Turbo Delphi.

user profile iconub60 hat folgendes geschrieben Zum zitierten Posting springen:
Allerdings ist so vom normalen Menschenverstand her der BIS-Wert immer eingeschlossen, auch wenn es anders in der Hilfe steht :twisted:
Die anderen Befehle sind aber auch so umgesetzt, und so ist es eigentlich in jeder (mir bekannten) Sprache soweit ich mich erinnere, dass die Angabe der oberen Grenze exklusive ist.
Java-Referenz hat folgendes geschrieben:
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)
.NET-Referenz hat folgendes geschrieben:
The exclusive upper bound of the random number returned.
Also warum sollte ausgerechnet Delphi bei einem von 3 Befehlen eine Ausnahme machen :?: