Pages

Sunday, April 08, 2012

20 Java Math.random() Uses

In this post, I have coded 20 Math.random manipulations in Java for you to access such as recieving a random card out of a deck of 64, selecting a random color (aprx. million colors), and some more simple applications like generating a percent (0% to 100%). Enjoy!
  1. Positive Integers (1 to 10)

    int result1 = (int)(Math.random()*10)+1; 
    
  2. Negative Integers (-1 to -10)

    int result2 = (int)((Math.random()*(-10))-1);
    
  3. Positive Integers (1 to 100)

    int result3 = (int)(Math.random()*100)+1; 
    
  4. Percent Values (0.00% to 100.00%)

    double result4 = (Math.random()*100); 
    
  5. One or Zero / Bit Value

    int result5 = ((int)(Math.random()*2));  
    
  6. Positive Odd Integers (1,3,5,7,9)

    int result6 = ((int)(Math.random()*9)+1); 
     while(result6%2!=1){ 
      result6 = ((int)(Math.random()*9)+1); 
     }  
    
  7. Positive Even Integers (2,4,6,8,10)

    int result = ((int)(Math.random()*10)+1); 
     while(result%2!=0){ 
      result = ((int)(Math.random()*10)+1); 
     } 
     
    
  8. Positive Multiples of Three (3,6,9,12,15,18, ... 54,57,60)

    int result = ((int)(Math.random()*10+1))*3; 
    
  9. Positive Multiples Of Four (4,8,12,16,20... 52,56,60)

    int result = (((int)(Math.random()*10))+1)*4; 
    System.out.println("mult of 4: "+result); 
    
  10. Perfect Squares (1,4,16,25,36,49,64,81,100,121)

    int result = (int)Math.pow(((int)(Math.random()*11)+1),2); 
    System.out.println("random perfect square: "+result); 
    
  11. Prime Numbers (1,3,5,7,11,13,17,19,23)

    int[] primes= {2,3,5,7,11,13,17,19,23}; 
    int randPrime = primes[(int)(Math.random()*primes.length)]; 
    System.out.println("random prime: "+randPrime); 
    
  12. Specific Set of Integers

    int[] possibleInts = {911,18,1,923,817,200}; 
    int result = possibleInts[(int)(Math.random()*possibleInts.length)]; 
    
  13. Specific Set of Strings

    String[] strings = 
    {"mango","pineapple", "orange", "apple", "banana", "kiwi"};
    String result = strings[(int)(Math.random()*strings.length)];
    
  14. Multiples of N (some integer N)

    public int randomMultipleOf(int N, int greatestFactor){ 
    return ((int)((Math.random()*greatestFactor)+1))*N; 
    //highest return value = N*greatestFactor 
    //in this example below it would be 7*9 = 63 maximum return value 
    randomMultipleOf(7,9); 
    
  15. Random year of the last 100 years

    int currentYear=2012;
    int result = currentYear + ((int)(Math.random()*100)+1); 
    
  16. Random Color (RGB)

    public Color randomColor(){ 
      return new Color((int)(Math.random()*256), (int)(Math.random()*256),(int)(Math.random()*256)); 
     } 
    
  17. Perfect Cubes

    public int randomPerfectCube(int minRoot, int maxRoot){ 
      return ((int)(Math.random()*maxRoot)+minRoot); 
     } 
    
  18. Random Card of a Standard Card Deck

    public String randomCard() { 
      int randSuit = (int)(Math.random()*4); 
      int randNumber =  (int)(Math.random()*14); 
      String suit, name; 
      if(randSuit==0) { 
       suit="Spades"; 
      } else if(randSuit==1) { 
       suit="Diamonds"; 
      } else if(randSuit==2) { 
       suit="Clubs"; 
      } else { 
       suit="Hearts"; 
      } 
      if((randNumber0)) { 
       name=randNumber+""; 
      } else if(randNumber==0) { 
       name="Ace"; 
      } else if(randNumber==11) { 
       name="Jack"; 
      } else if(randNumber==12) { 
       name="Queen"; 
      } else { 
       name="King"; 
      } 
      return name + " of  " + suit; 
    } 
    
  19. Letters of the English Alphabet

    public char randomLetter(){ 
       String letters = "abcdefghijklmnopqrstuvwxyz"; 
       return letters.charAt((int)(Math.random()*letters.length())); 
    } 
    
  20. Letter from a specific String parameter

    public char randomCharOf(String s) { 
         return s.charAt((int)(Math.random()*s.length())); 
     } 
    

Contacting Brian:
If there are any errors at all or if you want to suggest or request another manipulation email me at w7rdotblogspot@gmail.com or simply comment on this post.

No comments:

Post a Comment