/** * @return the randomSeed as a long */ private synchronized Long getRandomSeedAsLong() { Long seed = null; try { seed = Long.parseLong(randomSeed); } catch (NumberFormatException e) { if(log.isWarnEnabled()) { log.warn("Cannot parse random seed: '{}' in element {}", randomSeed, getName()); } } return seed; }
private String formatNumber(long value){ String format = getOutputFormat(); if (format != null && format.length() > 0) { try { DecimalFormat myFormatter = new DecimalFormat(format); return myFormatter.format(value); } catch (IllegalArgumentException ignored) { log.warn("Exception formatting value: {} at format: {}, using default", value, format); } } return Long.toString(value); }
private Random createRandom() { if (randomSeed.length()>0){ Long seed = getRandomSeedAsLong(); if(seed != null) { return new Random(seed.longValue()); } } return new Random(); }
/** {@inheritDoc} */ @Override public void iterationStart(LoopIterationEvent iterEvent) { Random randGen; if (getPerThread()){ randGen = perThreadRandom.get(); } else { synchronized(this){ if (globalRandom == null){ init(); globalRandom = createRandom(); } randGen=globalRandom; } } if (range <=0){ return; } long nextRand = minimum + randGen.nextInt(range); // Cannot use getThreadContext() as we are not cloned per thread JMeterVariables variables = JMeterContextService.getContext().getVariables(); variables.put(getVariableName(), formatNumber(nextRand)); }
private void init(){ final String minAsString = getMinimumValue(); minimum = NumberUtils.toLong(minAsString); final String maxAsString = getMaximumValue(); long maximum = NumberUtils.toLong(maxAsString); long rangeL=maximum-minimum+1; // This can overflow if (minimum > maximum){ log.error("maximum({}) must be >= minimum({})", maxAsString, minAsString); range=0;// This is used as an error indicator return; } if (rangeL > Integer.MAX_VALUE || rangeL <= 0){// check for overflow too log.warn("maximum({}) - minimum({}) must be <= {}", maxAsString, minAsString, Integer.MAX_VALUE); rangeL=Integer.MAX_VALUE; } range = (int)rangeL; }