# Random number generator to populate fields in a repeater

Hi,

I am wondering if there is a way to populate some text strings that should contain a number value using a random generator for a range; example from 50 - 5000 or even 1.0 - 10.0 (including 1 decimal place).

Or if anyone knows a function I can use in excel that would already be one step. Would be more ideal if it can be done directly in axure though. Any idea if and how this might be possible please?

Thank you!

Hi!

The function `Math.random()` returns a number between 0 and 1 (e.g., 0.275), so for 50 to 5000 the expression youâ€™d use in the `Item Loaded` event is:

``````Set text of whatever to [[(50 + 4500 * Math.random()).toFixed(0)]]
``````

(The number in the `toFixed()` function is the number of decimal places you want the number rounded to.)

For 1.0 to 10.0 youâ€™d sayâ€¦

``Set text of whatever to [[(1 + Math.random() * 9).toFixed(1)]]``
1 Like

Awesome, I have a follow up question;

Example:
I have to load two separate number values in the repeater:
Value 1: Primary number (e.g. Official price)
Value 2: Secondary number (e.g. Sale price). This number must be (e.g. 1 - 50 values) lower than the primary number.

How would one go about setting up both numbers with the Math.random function?

Hi!

So this is assuming you are not using column values to set the prices, correct?

You would use two set text commands: the first would set the normalPrice widget to a random value, and the second set text would set the salePrice to the value of the normalPrice widget minus a random number between 1 and 50.

You will need to use a local variable to grab the value of the normalPrice widget in the second set text command:

``````On Item Loaded
Set text of normalPrice to [[(50 + 4500 * Math.random()).toFixed(0)]]
Set text of salePrice to [[ LVAR_normalPrice - (1 + 49 * Math.random()).toFixed(0) ]]
``````

â€¦where LVAR_normalPrice is the local variable referring to the text of the normalPrice widget.

1 Like

Hi again,

the first command is working however the second set text command is returning NaN instead of a value. Could this be a result of the case I have set up? or because of its use with rich text?

This is a sample of the actual file Iâ€™m working with.
Filters_Price.rp (8.4 MB)

Hi!

The issue is that you are attaching the dollar sign directly to the numeric value, and the second expression is trying to do math on something that contains a non-numeric character.

Two ways to handle this:

• Put the dollar sign in a separate text widget to the left of the number you create (without the dollar sign)
• Use an expression to strip out the dollar sign before trying to subtract from it.

Iâ€™d recommend the former just in the spirit of keeping things simple, but the latter goes like this:

``````[[LVAR_ChampionPrice.replace('\$', '') - (1 + 49 * Math.random()).toFixed(0) ]]
``````

The replace() function replaces the its first parameter in the string with its second parameter: here, a dollar sign with nothing.

IMPORTANT: Since this expression involves quotes, make sure you arenâ€™t using â€śsmart quotes.â€ť When you look closely at the single quotes or double quotes, they should be straight up-and-down instead of hooked. If youâ€™re using MacOs, turn off smart quotes in the system preferences under Keyboard > Text.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.