You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RandBetween.java 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.record.formula.atp;
  16. import org.apache.poi.hssf.record.formula.eval.ErrorEval;
  17. import org.apache.poi.hssf.record.formula.eval.EvaluationException;
  18. import org.apache.poi.hssf.record.formula.eval.NumberEval;
  19. import org.apache.poi.hssf.record.formula.eval.OperandResolver;
  20. import org.apache.poi.hssf.record.formula.eval.ValueEval;
  21. import org.apache.poi.hssf.record.formula.functions.FreeRefFunction;
  22. import org.apache.poi.ss.formula.OperationEvaluationContext;
  23. /**
  24. * Implementation of Excel 'Analysis ToolPak' function RANDBETWEEN()<br/>
  25. *
  26. * Returns a random integer number between the numbers you specify.<p/>
  27. *
  28. * <b>Syntax</b><br/>
  29. * <b>RANDBETWEEN</b>(<b>bottom</b>, <b>top</b>)<p/>
  30. *
  31. * <b>bottom</b> is the smallest integer RANDBETWEEN will return.<br/>
  32. * <b>top</b> is the largest integer RANDBETWEEN will return.<br/>
  33. * @author Brendan Nolan
  34. */
  35. final class RandBetween implements FreeRefFunction{
  36. public static final FreeRefFunction instance = new RandBetween();
  37. private RandBetween() {
  38. //enforces singleton
  39. }
  40. /**
  41. * Evaluate for RANDBETWEEN(). Must be given two arguments. Bottom must be greater than top.
  42. * Bottom is rounded up and top value is rounded down. After rounding top has to be set greater
  43. * than top.
  44. *
  45. * @see org.apache.poi.hssf.record.formula.functions.FreeRefFunction#evaluate(org.apache.poi.hssf.record.formula.eval.ValueEval[], org.apache.poi.ss.formula.OperationEvaluationContext)
  46. */
  47. @Override
  48. public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
  49. double bottom, top;
  50. if (args.length != 2) {
  51. return ErrorEval.VALUE_INVALID;
  52. }
  53. try {
  54. bottom = OperandResolver.coerceValueToDouble(OperandResolver.getSingleValue(args[0], ec.getRowIndex(), ec.getColumnIndex()));
  55. top = OperandResolver.coerceValueToDouble(OperandResolver.getSingleValue(args[1], ec.getRowIndex(), ec.getColumnIndex()));
  56. if(bottom > top) {
  57. return ErrorEval.NUM_ERROR;
  58. }
  59. } catch (EvaluationException e) {
  60. return ErrorEval.VALUE_INVALID;
  61. }
  62. bottom = Math.ceil(bottom);
  63. top = Math.floor(top);
  64. if(bottom > top) {
  65. top = bottom;
  66. }
  67. return new NumberEval((bottom + (int)(Math.random() * ((top - bottom) + 1))));
  68. }
  69. }