G
Guest
Gast
Hallo,
wie kann ich am besten eine Zufallsvariable im Intervall [1.0,8.0] erzeugen.
wie kann ich am besten eine Zufallsvariable im Intervall [1.0,8.0] erzeugen.
package main.java;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Random;
import java.util.Set;
public class Test {
public static void main(String[] args) {
HashMap<Integer, Integer> counts = new HashMap<Integer, Integer>();
Random rnd = new Random();
for(int i = 0; i< 1000000; i++){
float value = (rnd.nextFloat()*10)%8;
Integer count =counts.get((int) value);
counts.put((int) value, count==null?1:count+1);
}
Set<Integer> keys = counts.keySet();
ArrayList<Integer> keyList = new ArrayList<Integer>(keys);
Collections.sort(keyList);
for(Integer key: keyList)
System.out.println(key+": "+counts.get(key));
System.out.println("besser");
counts = new HashMap<Integer, Integer>();
rnd = new Random();
for(int i = 0; i< 1000000; i++){
float value = rnd.nextFloat()*8f;
Integer count =counts.get((int) value);
counts.put((int) value, count==null?1:count+1);
}
keys = counts.keySet();
keyList = new ArrayList<Integer>(keys);
Collections.sort(keyList);
for(Integer key: keyList)
System.out.println(key+": "+counts.get(key));
}
}
int i = random.nextInt();
double d = i / (double)(Integer.MAX_VALUE-1);
float f = (float)(d*8);
public class Test {
public static void main(String[] args) {
x(Integer.MAX_VALUE);
x(Integer.MAX_VALUE - 1);
x(Integer.MAX_VALUE - 2);
x(Integer.MAX_VALUE - 3);
x(Integer.MAX_VALUE - 30);
x(Integer.MAX_VALUE - 60);
x(Integer.MAX_VALUE - 63);
x(Integer.MAX_VALUE - 65);
x(Integer.MAX_VALUE - 66);
x(Integer.MAX_VALUE - 67);
x(Integer.MAX_VALUE - 75);
x(Integer.MAX_VALUE - 90);
x(Integer.MAX_VALUE - 100);
x(Integer.MAX_VALUE - 300);
x(Integer.MAX_VALUE - 3000);
x(Integer.MAX_VALUE - 3001);
x(Integer.MAX_VALUE - 3002);
x(Integer.MAX_VALUE - 3003);
x(Integer.MAX_VALUE - 3004);
x(Integer.MAX_VALUE - 3005);
x(Integer.MAX_VALUE - 3006);
x(Integer.MAX_VALUE - 3007);
x(Integer.MAX_VALUE - 3008);
x(Integer.MAX_VALUE - 3009);
x(Integer.MAX_VALUE - 3010);
x(Integer.MAX_VALUE - 3011);
x(Integer.MAX_VALUE - 3021);
x(1000003);
x(1000002);
x(1000001);
x(100001);
x(10001);
x(1000);
x(100);
x(10);
x(1);
x(0);
}
static void x(int i) {
double d = i / (double) (Integer.MAX_VALUE - 1);
float f = (float) (d * 8);
System.out.println(f + " -- " + i + " -- " + (Integer.MAX_VALUE - i));
}
}
-------
8.0 -- 2147483647 -- 0
8.0 -- 2147483646 -- 1
8.0 -- 2147483645 -- 2
8.0 -- 2147483644 -- 3
8.0 -- 2147483617 -- 30
8.0 -- 2147483587 -- 60
8.0 -- 2147483584 -- 63
8.0 -- 2147483582 -- 65
7.9999995 -- 2147483581 -- 66
7.9999995 -- 2147483580 -- 67
7.9999995 -- 2147483572 -- 75
7.9999995 -- 2147483557 -- 90
7.9999995 -- 2147483547 -- 100
7.999999 -- 2147483347 -- 300
7.999989 -- 2147480647 -- 3000
7.999989 -- 2147480646 -- 3001
7.999989 -- 2147480645 -- 3002
7.999989 -- 2147480644 -- 3003
7.999989 -- 2147480643 -- 3004
7.999989 -- 2147480642 -- 3005
7.999989 -- 2147480641 -- 3006
7.999989 -- 2147480640 -- 3007
7.999989 -- 2147480639 -- 3008
7.9999886 -- 2147480638 -- 3009
7.9999886 -- 2147480637 -- 3010
7.9999886 -- 2147480636 -- 3011
7.9999886 -- 2147480626 -- 3021
0.0037253015 -- 1000003 -- 2146483644
0.0037252977 -- 1000002 -- 2146483645
0.003725294 -- 1000001 -- 2146483646
3.7253276E-4 -- 100001 -- 2147383646
3.725663E-5 -- 10001 -- 2147473646
3.7252903E-6 -- 1000 -- 2147482647
3.7252903E-7 -- 100 -- 2147483547
3.7252903E-8 -- 10 -- 2147483637
3.7252903E-9 -- 1 -- 2147483646
0.0 -- 0 -- 2147483647
SlaterB hat gesagt.:interessanter Gedankte, aber was hast du gegen Integer.MAX_VALUE?
und auch negative Zahlen muss man bedenken
SlaterB hat gesagt.:da haben aber Zahlen zwischen 0 und 2 eine doppelt so Chance wie die anderen,
alle Zahlen über 8.0 müssen verworfen werden, dann per Schleife eine neue Zufallszahl erstellen
weniger 'Verlust' gibts bei random.nextDouble() * 8.0001,
bisschen Verlust aber immer noch und evtl. Probleme durch die Berechnung
edit:
das +1 fehlt auch noch, also alles über 7 weg und dann +1
und random.nextInt(MAX) würde sicherlich 0 bis MAX-1 geben, aber das muss ja nicht für nextInt() selber auch geltenMarco13 hat gesagt.:random.nextInt(100) liefert maximal 99 - damit man 1.0 bekommt muss man durch 99 teilen
rand=randomizer.nextFloat()*Math.nextUp(up-down)+down;
import java.util.*;
public class TEST {
public static void main(String... _){
float up=8f, down=1f;
float rand;
boolean upFlag=false, downFlag=false;
Random randomizer=new Random(1234567);
for(int i=0; i<1000000000; i++){
rand=randomizer.nextFloat()*Math.nextUp(up-down)+down;
upFlag|=(rand==up);
downFlag|=(rand==down);
if(rand>up){
System.out.println(">up"); break;
}
if(rand<down){
System.out.println("<down"); break;
}
}
System.out.println("up: "+upFlag+" down: "+downFlag);
}
}
System.out.println(7d+" != "+Math.nextUp(7d));
7.0 != 7.000000000000001