Skocz do zawartości
  • 👋 Witaj na MPCForum!

    Przeglądasz forum jako gość, co oznacza, że wiele świetnych funkcji jest jeszcze przed Tobą! 😎

    • Pełny dostęp do działów i ukrytych treści
    • Możliwość pisania i odpowiadania w tematach
    • System prywatnych wiadomości
    • Zbieranie reputacji i rozwijanie swojego profilu
    • Członkostwo w jednej z największych społeczności graczy

    👉 Dołączenie zajmie Ci mniej niż minutę – a zyskasz znacznie więcej!

    Zarejestruj się teraz

Random Bukkit.


Rekomendowane odpowiedzi

Opublikowano

Witam jestem nowy w java. Jak pobrac randomowy string z jakies listy ? Prosze o odp

z listy?

ogólnie potrzebujesz gdzies instancji Random-a, nigdy nie rób jej tylko po to by wylosować kilka cyferek, zrób np jedną.

private static final Random rand = new Random();

i wtedy

list.get(rand.nextInt(list.size()))

 

gdzie Random#nextInt(X) zwraca losową liczbę od 0 do X (ale bez X)

 

Takie moje utilsy od Random-a z kilkoma podstawowymi metodami

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.apache.commons.lang3.Validate; // zmień z lang na lang3 jak wywali błędy o braku tej klasy

public final class RandomUtils
{
    private static final Random random = new Random();

    private RandomUtils()
    {
    }

    public static Random getRandom()
    {
        return random;
    }

    public static <T> T getRand(final T[] array)
    {
        return getRand(random, array);
    }

    public static <T> T getRand(final List<T> coll)
    {
        return getRand(random, coll);
    }

    public static <T, E extends Collection<T>> E getRand(final Collection<T> coll, final E target, final int amount)
    {
        return getRand(random, coll, target, amount, true);
    }

    public static <T, E extends Collection<T>> E getRand(final Collection<T> coll, final E target, final int amount, final boolean noRepeat)
    {
        return getRand(random, coll, target, amount, noRepeat);
    }

    public static <T> T getRand(final Collection<T> coll)
    {
        return getRand(random, coll);
    }

    public static long getRandLongSafe(final long a, final long 
    {
        return getRandLongSafe(random, a, ;
    }

    public static int getRandIntSafe(final int a, final int 
    {
        return getRandIntSafe(random, a, ;
    }

    public static double getRandDoubleSafe(final double a, final double 
    {
        return getRandDoubleSafe(random, a, ;
    }

    public static float getRandFloatSafe(final float a, final float 
    {
        return getRandFloatSafe(random, a, ;
    }

    public static long getRandLong(final long min, final long max) throws IllegalArgumentException
    {
        return getRandLong(random, min, max);
    }

    public static int getRandInt(final int min, final int max) throws IllegalArgumentException
    {
        return getRandInt(random, min, max);
    }

    public static double getRandDouble(final double min, final double max) throws IllegalArgumentException
    {
        return getRandDouble(random, min, max);
    }

    public static float getRandFloat(final float min, final float max) throws IllegalArgumentException
    {
        return getRandFloat(random, min, max);
    }

    public static boolean getChance(final double chance)
    {
        return getChance(random, chance);
    }


    // custom random
    public static <T> T getRand(final Random random, final T[] array)
    {
        if (array.length == 0)
        {
            return null;
        }
        return array[random.nextInt(array.length)];
    }

    public static <T> T getRand(final Random random, final List<T> coll)
    {
        return coll.get(random.nextInt(coll.size()));
    }

    public static <T, E extends Collection<T>> E getRand(final Random random, final Collection<T> coll, final E target, final int amount)
    {
        return getRand(random, coll, target, amount, true);
    }

    public static <T, E extends Collection<T>> E getRand(final Random random, final Collection<T> coll, final E target, int amount, final boolean noRepeat)
    {
        if (coll.isEmpty())
        {
            return target;
        }
        final List<T> list = new ArrayList<>(coll);
        if (noRepeat)
        {
            while (! list.isEmpty() && (amount-- > 0))
            {
                target.add(list.remove(random.nextInt(list.size())));
            }
        }
        else
        {
            while (! list.isEmpty() && (amount-- > 0))
            {
                target.add(list.get(random.nextInt(list.size())));
            }
        }
        return target;
    }

    public static <T> T getRand(final Random random, final Collection<T> coll)
    {
        if (coll.isEmpty())
        {
            return null;
        }

        final int index = random.nextInt(coll.size());
        if (coll instanceof List)
        {
            return ((List<? extends T>) coll).get(index);
        }
        else
        {
            final Iterator<? extends T> iter = coll.iterator();
            for (int i = 0; i < index; i++)
            {
                iter.next();
            }
            return iter.next();
        }
    }

    public static long getRandLongSafe(final Random random, final long a, final long 
    {
        if (a > 
        {
            return getRandLong(random, b, a);
        }
        return getRandLong(random, a, ;
    }

    public static int getRandIntSafe(final Random random, final int a, final int 
    {
        return (int) getRandLongSafe(random, a, ;
    }

    public static double getRandDoubleSafe(final Random random, final double a, final double 
    {
        if (a > 
        {
            return getRandDouble(random, b, a);
        }
        return getRandDouble(random, a, ;
    }

    public static float getRandFloatSafe(final Random random, final float a, final float 
    {
        if (a > 
        {
            return getRandFloat(random, b, a);
        }
        return getRandFloat(random, a, ;
    }

    public static long getRandLong(final Random random, final long min, final long max) throws IllegalArgumentException
    {
        if (min == max)
        {
            return max;
        }
        Validate.isTrue(max > min, "Max can't be smaller than min!");
        return (Math.abs(random.nextLong()) % ((max - min) + 1)) + min;
    }

    public static int getRandInt(final Random random, final int min, final int max) throws IllegalArgumentException
    {
        if (min == max)
        {
            return max;
        }
        Validate.isTrue(max > min, "Max can't be smaller than min!");
        return (int) getRandLong(random, min, max);
    }

    public static double getRandDouble(final Random random, final double min, final double max) throws IllegalArgumentException
    {
        if (Double.compare(min, max) == 0)
        {
            return max;
        }
        Validate.isTrue(max > min, "Max can't be smaller than min!");
        return (random.nextDouble() * (max - min)) + min;
    }

    public static float getRandFloat(final Random random, final float min, final float max) throws IllegalArgumentException
    {
        if (Float.compare(min, max) == 0)
        {
            return max;
        }
        Validate.isTrue(max > min, "Max can't be smaller than min!");
        return (random.nextFloat() * (max - min)) + min;
    }

    public static boolean getChance(final Random random, final double chance)
    {
        return (chance > 0) && ((chance >= 100) || (chance >= getRandDouble(random, 0, 100)));
    }
}

Uzywan nextLong podczas generowania int-a by mieć pewność że zadziała nawet dla dziwnych zakresów przy bardzo małych/duzych liczbach

 

a samo losowanie longa juz niestety sobie nie poradzi z pełnym zakresem, ale Random i tak uzywa tylko 48 bitów z seeda więc i tak by trzeba coś innego wykombinować

1438614356923701010629.png

 

Zarchiwizowany

Ten temat przebywa obecnie w archiwum. Dodawanie nowych odpowiedzi zostało zablokowane.

×
×
  • Dodaj nową pozycję...