generate short random number in java?

12,927

Solution 1

There is no Random.nextShort() method, so you could use

short s = (short) Random.nextInt(Short.MAX_VALUE + 1);

The +1 is because the method returns a number up to the number specified (exclusive). See here

This will generate numbers from 0 to Short.MAX_VALUE inclusive (negative numbers were not requested by the OP)

Solution 2

The most efficient solution which can produce all possible short values is to do either.

short s = (short) random.nextInt(1 << 16); // any short
short s = (short) random.nextInt(1 << 15); // any non-negative short

or even faster

class MyRandom extends Random {
    public short nextShort() {
        return (short) next(16); // give me just 16 bits.
    }
    public short nextNonNegativeShort() {
        return (short) next(15); // give me just 15 bits.
    }
}

short s = myRandom.nextShort();

Solution 3

Java shorts are included in the -32 768 → +32 767 interval.

why wouldn't you perform a

Random.nextInt(65536) - 32768

and cast the result into a short variable ?

Solution 4

How about short s = (short) Random.nextInt();? Note that the resulting distribution might have a bias. The Java Language Specification guarantees that this will not result in an Exception, the int will be truncated to fit in a short.

EDIT

Actually doing a quick test, the resulting distribution seems to be uniformly distributed too.

Solution 5

Simply generate an int like:

 short s = (short)Random.nextInt(Short.MAX_VALUE);

The generated int will be in the value space of short, so it can be cast without data loss.

Share:
12,927
waqas
Author by

waqas

Updated on June 19, 2022

Comments

  • waqas
    waqas about 2 years

    I want to generate a random number of type short exactly like there is a function for integer type called Random.nextInt(134116). How can I achieve it?

  • luketorjussen
    luketorjussen about 12 years
    what if the number generated is larger than be held in a short?
  • Tudor
    Tudor about 12 years
    But if you add 1 you won't be able to generate 0?
  • luketorjussen
    luketorjussen about 12 years
    @Tudor, the +1 is added to Short.MAX_VALUE, not the result of nextInt so it will generate a number between 0 and Short.MAX_VALUE inclusive.
  • assylias
    assylias about 12 years
    only the last four bits will be retained, but it will not generate an overflow.
  • Skippy Fastol
    Skippy Fastol about 12 years
    I guess it and wrote it for you so you would extend the lifespan of your keyboard.
  • Skippy Fastol
    Skippy Fastol about 12 years
    but will it alter the fine-tuned uniformity of the generated random numbers ?
  • Vishy
    Vishy about 12 years
    That will generate non-negative short values.
  • Vishy
    Vishy about 12 years
    That will generate non-negative short values, except Short.MAX_VALUE
  • Vishy
    Vishy about 12 years
    All the lowest 16 bits are euqally likely. There will be no more bias after a cast than before.
  • luketorjussen
    luketorjussen about 12 years
    @PeterLawrey, which is what the OP has asked for :)
  • waqas
    waqas about 12 years
    @all what if I also want to have a limit also like to Random.nextInt(23456) will generate any number from 0-23456. How can make it possible with short inputs?
  • luketorjussen
    luketorjussen about 12 years
    @Waqas, if you want a short to be returned you have to cast it no matter what value you set as your limit. The limit just tells random the maximum value of the integer you want it to return is.
  • mwengler
    mwengler over 11 years
    random.nextInt() produces an integer distributed uniformly from Integer.MIN_VALUE to Integer.MAX_VALUE, that is, about an equal number of values less than 0 as are greater than 0. On the other hand, the proposed solution Random.nextInt(Short.MAX_VALUE + 1) produces only values greater than 0, none less than 0.
  • luketorjussen
    luketorjussen over 11 years
    @mwengler - see comments on question - "do you need negative numbers?" "no luke I dont need negatives only from 0 max short"
  • mwengler
    mwengler over 11 years
    @luketorjussen OK I see what happened. Above the convention is followed of referring to a method as .nextInteger(int top) as .nextInteger(). But Random.nextInteger() with no argument gives positive and negative numbers while Random.nextInteger(top) gives only positive. I think it valuable to leave that explicit for others who read this later.
  • fIwJlxSzApHEZIl
    fIwJlxSzApHEZIl over 7 years
    @luketorjussen cannot make a static references to the non static method nextInt(). You need to instantiate an instance of Random() first.