@Test public void testNegativeUpperBoundIsHonored() throws Exception { final Random random = new Random(); final RandomInt ri = new RandomInt(random); for(int i = 0; i < 5000; ++i) { final Symbol result = ri.execute(rhsFuncContext, Symbols.asList(syms, -10)); assertNotNull(result); assertNotNull(result.asInteger()); final long value = result.asInteger().getValue(); assertTrue(value <= 0 && value > -10); } }
@Override public Symbol execute(RhsFunctionContext context, List<Symbol> arguments) throws RhsFunctionException { RhsFunctions.checkArgumentCount(this, arguments); if(arguments.size() == 0) { return context.getSymbols().createInteger(random.nextInt()); } else { final IntegerSymbol maxSym = arguments.get(0).asInteger(); if(maxSym == null) { throw new RhsFunctionException("random-int: Expected integer for first argument, got " + arguments.get(0)); } final long max = maxSym.getValue(); if(max >= 0) { return context.getSymbols().createInteger(random.nextInt((int)max)); } else { return context.getSymbols().createInteger(-random.nextInt(-(int) max)); } } }
RandomInt extends AbstractRhsFunctionHandler { @Override public Symbol execute(RhsFunctionContext context, List<Symbol> arguments) throws RhsFunctionException { RhsFunctions.checkArgumentCount(this, arguments); if(arguments.size() == 0) { return context.getSymbols().createInteger(random.nextInt()); } else { final IntegerSymbol maxSym = arguments.get(0).asInteger(); if(maxSym == null) { throw new RhsFunctionException("random-int: Expected integer for first argument, got " + arguments.get(0)); } final long max = maxSym.getValue(); if(max >= 0) { return context.getSymbols().createInteger(random.nextInt((int)max)); } else { return context.getSymbols().createInteger(-random.nextInt(-(int) max)); } } } }
RandomInt extends AbstractRhsFunctionHandler { @Override public Symbol execute(RhsFunctionContext context, List<Symbol> arguments) throws RhsFunctionException { RhsFunctions.checkArgumentCount(this, arguments); if(arguments.size() == 0) { return context.getSymbols().createInteger(random.nextInt()); } else { final IntegerSymbol maxSym = arguments.get(0).asInteger(); if(maxSym == null) { throw new RhsFunctionException("random-int: Expected integer for first argument, got " + arguments.get(0)); } final long max = maxSym.getValue(); if(max >= 0) { return context.getSymbols().createInteger(random.nextInt((int)max)); } else { return context.getSymbols().createInteger(-random.nextInt(-(int) max)); } } } RandomInt(Random random); }
RandomInt extends AbstractRhsFunctionHandler { @Override public Symbol execute(RhsFunctionContext context, List<Symbol> arguments) throws RhsFunctionException { RhsFunctions.checkArgumentCount(this, arguments); if(arguments.size() == 0) { return context.getSymbols().createInteger(random.nextInt()); } else { final IntegerSymbol maxSym = arguments.get(0).asInteger(); if(maxSym == null) { throw new RhsFunctionException("random-int: Expected integer for first argument, got " + arguments.get(0)); } final long max = maxSym.getValue(); if(max >= 0) { return context.getSymbols().createInteger(random.nextInt((int)max)); } else { return context.getSymbols().createInteger(-random.nextInt(-(int) max)); } } } RandomInt(Random random); @Override Symbol execute(RhsFunctionContext context, List<Symbol> arguments); }
RandomInt extends AbstractRhsFunctionHandler { @Override public Symbol execute(RhsFunctionContext context, List<Symbol> arguments) throws RhsFunctionException { RhsFunctions.checkArgumentCount(this, arguments); if(arguments.size() == 0) { return context.getSymbols().createInteger(random.nextInt()); } else { final IntegerSymbol maxSym = arguments.get(0).asInteger(); if(maxSym == null) { throw new RhsFunctionException("random-int: Expected integer for first argument, got " + arguments.get(0)); } final long max = maxSym.getValue(); if(max >= 0) { return context.getSymbols().createInteger(random.nextInt((int)max)); } else { return context.getSymbols().createInteger(-random.nextInt(-(int) max)); } } } RandomInt(Random random); @Override Symbol execute(RhsFunctionContext context, List<Symbol> arguments);  }