Created
January 24, 2012 11:44
-
-
Save opqdonut/1669763 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Tämä on yksinkertaisempi mutta generoi nollia | |
import java.util.Random; | |
import java.util.Arrays; | |
public class Lol { | |
public static void main(String[] args) { | |
int K=10; | |
int N=10; | |
for (int i=0; i<K; i++) { | |
generoi(N); | |
} | |
} | |
public static void generoi(int montako) { | |
int summa=666; | |
int[] taul=new int[montako]; | |
Random r = new Random(); | |
int i; | |
for (i=0; i<montako; i++) { | |
int luku = r.nextInt(summa); | |
taul[i]=luku; | |
summa=summa-luku; | |
} | |
System.out.println(Arrays.toString(taul)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Tämä ei generoi nollia | |
import java.util.Random; | |
import java.util.Arrays; | |
public class Lol { | |
public static void main(String[] args) { | |
int K=10; | |
int N=10; | |
for (int i=0; i<K; i++) { | |
generoi(N); | |
} | |
} | |
public static void generoi(int montako) { | |
while (true) { // Nyt pitää yrittää monta kertaa koska saatetaan käyttää 666 loppuun liian aikaisin | |
int summa=666; | |
int[] taul=new int[montako]; | |
Random r = new Random(); | |
int i; | |
for (i=0; i<montako; i++) { | |
if (summa<=0) | |
break; // koitetaan uudestaan! | |
int luku = r.nextInt(summa)+1; | |
taul[i]=luku; | |
summa=summa-luku; | |
} | |
if (i==montako) { // saatiin valmiiksi | |
System.out.println(Arrays.toString(taul)); | |
break; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Tämä ei generoi nollia myöskään, mutta tulosteet tuppaavat olemaan | |
// vähän tylsiä: päättyvät usein 1,1,1,1 | |
import java.util.Random; | |
import java.util.Arrays; | |
public class Lol { | |
public static void main(String[] args) { | |
int K=10; | |
int N=10; | |
for (int i=0; i<K; i++) { | |
generoi(N); | |
} | |
} | |
public static void generoi(int montako) { | |
int summa=666; | |
int[] taul=new int[montako]; | |
Random r = new Random(); | |
int i; | |
for (i=0; i<montako; i++) { | |
int jaljella = montako-i; | |
int luku = r.nextInt(summa-jaljella)+1; | |
taul[i]=luku; | |
summa=summa-luku; | |
} | |
System.out.println(Arrays.toString(taul)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment