Autor Beitrag
NetZwerg
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 448

Slackware 10.1
Kylix 3, D5 Standard
BeitragVerfasst: So 04.04.04 22:55 
Hallo,

ich möchte zwei Zufalls-Integer werte aneinander reihen... Ich möchte sie allerdings nicht addieren. Wie mache ich das?

ausblenden Delphi-Quelltext
1:
Integer := (Random(4) + Random(4);					


Würde ja nur bewirken, das irgendwas zwischen 0 und 6 rauskommt. Ich möchte aber das zum Beispiel 35 oder 27 rauskommt!?

Tschüss, NetZwerg :nixweiss:

_________________
"Manche Leute drücken nur deshalb ein Auge zu, damit sie besser zielen können."
Billy Wilder, amer. Filmregisseur
toms
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1099
Erhaltene Danke: 2



BeitragVerfasst: So 04.04.04 22:57 
Hi,

Dazu musst du zuerst beide Integer Werte in einen String umwandeln
und das Resultat wieder zurück in ein Integer.

Also so:

ausblenden Delphi-Quelltext
1:
Integer := StrToInt(IntToStr(Random(4)) +IntToStr(Random(3)))					
NetZwerg Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 448

Slackware 10.1
Kylix 3, D5 Standard
BeitragVerfasst: So 04.04.04 22:58 
aber es geht irgendwie nich...

ausblenden Delphi-Quelltext
1:
Should := StrToInt(IntToStr(Random(3))) + StrToInt(IntToStr(Random(3)));					


Should is mein integerwert. Dann kommt immer nur 2, 5 oder so raus!?

_________________
"Manche Leute drücken nur deshalb ein Auge zu, damit sie besser zielen können."
Billy Wilder, amer. Filmregisseur
NetZwerg Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 448

Slackware 10.1
Kylix 3, D5 Standard
BeitragVerfasst: So 04.04.04 23:09 
oh, hab meinen fehler!

Ich muss ja nur einmal StrToInt() und zwischen die klammern muss das ganze gedöns!

ausblenden Delphi-Quelltext
1:
Should := StrToInt(IntToStr(Random(3)) + IntToStr(Random(3)));					

_________________
"Manche Leute drücken nur deshalb ein Auge zu, damit sie besser zielen können."
Billy Wilder, amer. Filmregisseur
bms
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 735

Win XP
Delphi 3, Delphi 6 PE, Delphi 2005 PE
BeitragVerfasst: So 04.04.04 23:21 
edit: war ich wohl zu langsam
Brainiac
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mo 05.04.04 09:19 
Hm... also für dein Beispiel würde mir aber noch was wesentlich einfacheres einfallen:

ausblenden Delphi-Quelltext
1:
Should := Random(3)*10 + Random(3);					
NetZwerg Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 448

Slackware 10.1
Kylix 3, D5 Standard
BeitragVerfasst: Mo 05.04.04 10:01 
ja, hat was... so könnte man's machen! Aber is ja gut das man ma weiß wie man sowas aneinanderreiht, jetz ma unabhängig von meinem beispiel... THX! :beer:

_________________
"Manche Leute drücken nur deshalb ein Auge zu, damit sie besser zielen können."
Billy Wilder, amer. Filmregisseur
MSCH
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1448
Erhaltene Danke: 3

W7 64
XE2, SQL, DevExpress, DevArt, Oracle, SQLServer
BeitragVerfasst: Mo 05.04.04 17:58 
noch einfacher :-)
da du nur Werte unterhalb des Wertebereiches eines Bytes erstellst (Random (3)...) wäre
auch gut, einfach die Bytes zu verschieben.
Pseudocode:
ausblenden Quelltext
1:
2:
3:
I:= Random(3);
I:= I shl 8;
I:= I and Byte(Random(2));


grez
msch

_________________
ist das politisch, wenn ich linksdrehenden Joghurt haben möchte?
raziel
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 2453

Arch Linux
JS (WebStorm), C#, C++/CLI, C++ (VS2013)
BeitragVerfasst: Mo 05.04.04 18:52 
ausblenden Quelltext
1:
2:
3:
I:= Random(3);
I:= I shl 8;
I:= I and Byte(Random(2));

hier gibs aber dann nur 7 verschiedene ergebnisse (genauso viel wie am anfang...)!

[edit]is doch eins ergebnis mehr... (hab die null vergessen)[/edit]

_________________
JSXGraph
tommie-lie
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 4373

Ubuntu 7.10 "Gutsy Gibbon"

BeitragVerfasst: Mo 05.04.04 20:14 
MSCH hat folgendes geschrieben:
noch einfacher :-)
da du nur Werte unterhalb des Wertebereiches eines Bytes erstellst (Random (3)...) wäre
auch gut, einfach die Bytes zu verschieben.
Das würde aber nicht wie gewünscht funktionieren (zumindest nicht im Dezimalsystem) :roll:
Da wir hier keine BCD-Zahlen benutzen, steht nicht ein Byte für eine Zahl, sondern das ganze Byte für Zahlen von 0 bis 255. Bei deiner Schiebungsaktion würde also keine zweistellige Dezimalzahl rauskommen, sondern... was anderes (zu faul zum Denken bin :mrgreen:).
Die Lösung von Brainiac:Should := Random(3)*10 + Random(3);ist also schon die einzig sinnvolle und kürzeste (man kann "*10" nicht mit einfachem shiften erreichen, und der MUL-Opcode dürfteda schon das schnellste sein).

_________________
Your computer is designed to become slower and more unreliable over time, so you have to upgrade. But if you'd like some false hope, I can tell you how to defragment your disk. - Dilbert