Random - Problem

Status
Nicht offen für weitere Antworten.
G

Gast

Gast
Hallo,
ich hab folgenden Code geschrieben um in JAVA-ME eine Zufallszahl zwischen 1 und "max" zu erzeugen.
Komischerweise kommen 2, 3, oder 9 sehr sehr häufig vor, andere dafür fast nie.
In der Konsole hatte ich bei verwendung von Random das selbe Problem (bei Math.random() dagegen nicht).

Kann es sein, dass das Verfahren (mit Systemzeit) etwas damit zu tun hat?
Wenn ja, wie kann ich das Problem beheben? Math.random gibt es ja bei ME leider nicht!

Code:
int zufallszahl(int max)
{
        Random random = new Random();
        int zahl1 = Math.abs(random.nextInt()/100), potenz = 1;
	    
        for (int i = 0; i < (String.valueOf(zahl1).length()); i++)
	potenz = potenz*10;
	    
	return (zahl1*max)/potenz;	
}

Gruß

Fleckenteufel

[Edit by Beni: es ist eine ME-Frage, also gehört es auch ins ME-Subforum ;-) ]
 

doctus

Bekanntes Mitglied
ich weiß zwar nicht genau, wie die random()-methoden aufgebaut sind, aber das manche zahlen öffter vorkommen ist eine sache der wahrscheinlichkeitsrechnung. je öffter du eine zufallszahl erstellst, desto mehr nähern sich die vorkommen der einzellnen zahlen an.
 
S

SlaterB

Gast
Math.random:
Code:
public static double random() {
        if (randomNumberGenerator == null) initRNG(); [Klasse Random, statisch]
        return randomNumberGenerator.nextDouble();
    }

wenn du ein Random-Objekt erzeugst, kannst du es bis in alle Ewigkeit benutzen und es wird immer gute Zahlen liefern

dagegen ständig neue Random-Objekte zu erzeugen ist einerseits unnötig,
anderseits vielleicht auch die Quelle für dein Problem,


bei mir scheint das im kurzen Test nicht aufzutreten,
mein Random (Java 1.5 wohl) benutzt die sehr genaue System.nanoTime() als Startwert,

aber es besteht die Gefahr, dass du mehrere gleiche Objekte erzeugst und dann die gleichen Zufallszahlen erhälst
 

Chris_1980

Bekanntes Mitglied
weiß ja nicht genau ob die Klasse Random alle Methoden hat wie in der großen Version, aber wenn kannst du auch so den Bereich zwischen 1 u. max einstellen:

Code:
private  Random random = new Random(); 



int zufallszahl(int max) 
{  
     int zahl1 = random.nextInt(max)+1;
     return zahl1;    
}
 
G

Gast

Gast
Hallo,
danke erst mal für die Antworten.
Ich werde den letzteren Code gleich mal testen. - Die Methode "random.nextInt(max)" gibt es erst seit CLDC 1.1, deshalb ist es mir gar nicht aufgefallen.

Gruß

Fleckenteufel
 
G

Gast

Gast
Hallo,
mit dem letzten Code hat es geklappt.
Einfach nur die Erstellung des Random-Objektes aus der Methode rauszunehmen hat komischerweise nicht gereicht.

Gruß

Fleckenteufel
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
R Android App mit Datenbankeinträgen und Math.random() Methode programmieren Android & Cross-Platform Mobile Apps 0
W Prüfen, ob App auf Gerät installiert ist Problem S10 Android & Cross-Platform Mobile Apps 11
W In App Purchase Problem? Android & Cross-Platform Mobile Apps 36
W Problem mit Android Studio Android & Cross-Platform Mobile Apps 0
T Android R.string.test+i Problem Android & Cross-Platform Mobile Apps 2
K Android to Pi | Websocket Problem Android & Cross-Platform Mobile Apps 3
N Intent und finish() Problem Android & Cross-Platform Mobile Apps 5
B Android App Programmierung Einsteiger Problem Android & Cross-Platform Mobile Apps 4
emeraldo Android Problem mit Bottomnavmenu Android & Cross-Platform Mobile Apps 10
I Das Problem mit der Tastatur... android:windowSoftInputMode="adjustPan" Android & Cross-Platform Mobile Apps 1
M Android App → Problem mit dem Speichern von einem Bitmap–Objekt. Android & Cross-Platform Mobile Apps 1
A Android Android Studio Emulator Problem Android & Cross-Platform Mobile Apps 1
S Android Studio Bluetooth App Problem Android & Cross-Platform Mobile Apps 6
J TicTacToe Problem bei kontrolle Android & Cross-Platform Mobile Apps 7
J Button array ID Problem Android & Cross-Platform Mobile Apps 2
M Problem bei Werteübergabe, MSQL verbindung Android & Cross-Platform Mobile Apps 3
S Android Problem mit Android Virtual Device erstellung. Android & Cross-Platform Mobile Apps 2
Anfänger2011 Text to Speech Problem Android & Cross-Platform Mobile Apps 1
S Android Android java onclick listener Problem Android & Cross-Platform Mobile Apps 9
A Android Problem mit ListView und OnItemClickListener.. Android & Cross-Platform Mobile Apps 10
K Problem mit arraylist und button Android & Cross-Platform Mobile Apps 16
R W-Lan Problem über Sockets Android & Cross-Platform Mobile Apps 1
P ViewPager Problem Android & Cross-Platform Mobile Apps 1
A Android Problem mit Video von Youtube abspielen Android & Cross-Platform Mobile Apps 4
A Android Problem mit Zurücktaste und ausgabe der Aktuellen Seite Android & Cross-Platform Mobile Apps 6
B Android Problem mit Soundwiedergabe Android & Cross-Platform Mobile Apps 2
T Android Android Sensor: Java Problem Android & Cross-Platform Mobile Apps 1
G Problem beim Rendern von 3D-Objekt Android & Cross-Platform Mobile Apps 0
L Android Gyroscope Sensor Problem Android & Cross-Platform Mobile Apps 2
S Android GPS Problem Android & Cross-Platform Mobile Apps 24
J Eclipse Emulator Problem Android & Cross-Platform Mobile Apps 1
J Eclipse Emulator Problem Android & Cross-Platform Mobile Apps 0
B Android Problem mit Rückgabewert Android & Cross-Platform Mobile Apps 13
L Android komisches Bitmap-Größe-Problem Android & Cross-Platform Mobile Apps 8
D Android Layout Problem Android & Cross-Platform Mobile Apps 2
R Problem mit View in ScrollView Android & Cross-Platform Mobile Apps 6
R Eclipse + AndroidSDK - Problem mit Referenzen Android & Cross-Platform Mobile Apps 6
M Problem mit setOnClickListener Android & Cross-Platform Mobile Apps 4
DaniSahne96 Problem beim Appdebuggen auf Smartphone Android & Cross-Platform Mobile Apps 3
P Android Problem beim Widget - Denkfehler ? Android & Cross-Platform Mobile Apps 2
M GCM IntentService Problem Android & Cross-Platform Mobile Apps 3
D Android Gallery Problem Android & Cross-Platform Mobile Apps 5
P Problem mit Cell id Android & Cross-Platform Mobile Apps 6
L Android Problem mit "spinner" Android & Cross-Platform Mobile Apps 10
D Android problem mit geschwindigkeitsberechnung app Android & Cross-Platform Mobile Apps 2
E Android Problem mit Contact Provider Android & Cross-Platform Mobile Apps 1
H Android Problem mit ListActivity Android & Cross-Platform Mobile Apps 3
S Android Layout Problem mit fill_parent Android & Cross-Platform Mobile Apps 5
F Android ExpandableList, SimpleCursorTreeAdapter, Cursor Problem Android & Cross-Platform Mobile Apps 2
A Android Problem mit Long.getLong() bzw. Integer.getInteger() Android & Cross-Platform Mobile Apps 2
A Problem mit HTTP- Verbindung Android & Cross-Platform Mobile Apps 4
V [Java] und [JavaME] ClientServer StreamConnection . Problem beim lesen / schreiben Android & Cross-Platform Mobile Apps 2
F Eclipse JAD File erzeugen -- Problem Android & Cross-Platform Mobile Apps 10
R Ein Problem beim ausführen von folgendem Quelltext Android & Cross-Platform Mobile Apps 11
M Problem mit dem Auslesen von System Properties Android & Cross-Platform Mobile Apps 7
P wtk problem Android & Cross-Platform Mobile Apps 3
G Math exp() Problem Android & Cross-Platform Mobile Apps 4
G S40 Problem Android & Cross-Platform Mobile Apps 8
A Problem beim Subtrahieren eines Double von einem Double Android & Cross-Platform Mobile Apps 5
C Problem Device/Emulator wird nicht erkannt Android & Cross-Platform Mobile Apps 3
S Image Problem Android & Cross-Platform Mobile Apps 11
M Problem mit den Softkeys Android & Cross-Platform Mobile Apps 4
G J2ME jar-problem Android & Cross-Platform Mobile Apps 10
S Komisches Problem Android & Cross-Platform Mobile Apps 3
F Problem beim Erstellen der Jar File Android & Cross-Platform Mobile Apps 4
A Problem: Canvas-Grösse Motorola RAZR v3r Android & Cross-Platform Mobile Apps 8
S Problem mit Einbindung einer externer Bibliothek Android & Cross-Platform Mobile Apps 2
E problem mit den resourcen Android & Cross-Platform Mobile Apps 2
O Problem mit Datagramconnection Android & Cross-Platform Mobile Apps 2
P Problem mit der Uhrzeit Android & Cross-Platform Mobile Apps 2
S Problem auf dem Handy Android & Cross-Platform Mobile Apps 3

Ähnliche Java Themen

Neue Themen


Oben