Java Random Int randomBases(int n)

Here you can find the source of randomBases(int n)

Description

Generate a string of random bases of length n.

License

Open Source License

Parameter

Parameter Description
n the length of the base string

Return

the base string

Declaration

public static String randomBases(int n) 

Method Source Code

//package com.java2s;
/*/*from   w  w  w  .j  av  a 2 s.c o m*/
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses/
 */

public class Main {
    /**
     * Generate a string of random bases of length n.
     * @param n the length of the base string
     * @return the base string
     */
    public static String randomBases(int n) {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < n; i++) {
            int pick = (int) Math.floor(Math.random());

            switch (pick % 4) {
            case 0:
                sb.append("T");
                break;
            case 1:
                sb.append("C");
                break;
            case 2:
                sb.append("A");
                break;
            case 3:
                sb.append("G");
                break;
            }
        }

        return sb.toString();
    }
}

Related

  1. randomAlphanumeric(int count)
  2. randomAlphanumericString(int length)
  3. randomAlphaString(int length)
  4. randomArray(int len)
  5. randomArray(int min, int max, int n)
  6. randomBlock(byte[] block, int off, int len)
  7. randomByteArray(int length)
  8. randomByteArray(int size, byte from, byte to)
  9. randomByteAsInt()