Community Spring Cleaning week is here! Join your fellow Maveryx in digging through your old posts and marking comments on them as solved. Learn more here!

Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.
SOLVED

Generate Random Numbers

ts22
6 - Meteoroid

How would I assign a random number using rand or another function if I want the range to be between 0 to 16? 

9 REPLIES 9
AndrewBanh
9 - Comet

Hi @ts22 

 

Try the formula:

 

rand() * 16

 

The expression rand() generates a number between 0 - 1 so just multiply by 16, and set the data type as Int32 or something, so that there are no decimals.

 

If you want Double to be your Data type, then use something like ceil(rand() * 16) instead.

 

Hope that helps.

 

- Andrew

ts22
6 - Meteoroid

Thank you, I appreciate it! 

MarqueeCrew
20 - Arcturus
20 - Arcturus
A slightly different solution:

RandInt(n): Returns a random integer between 0 and the specified parameter

RandInt(10) returns 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 or 10

RandInt(5000) returns a random whole number between 0 and 5000, such as 741

Cheers,

Mark
Alteryx ACE & Top Community Contributor

Chaos reigns within. Repent, reflect and restart. Order shall return.
Please Subscribe to my youTube channel.
ts22
6 - Meteoroid

Is there a way to use RandInt and not have it assign duplicate values? I noticed it will give me two 8's instead of just a singular 0-16 for each number. 

AndrewBanh
9 - Comet

Hi @ts22 

 

I don't have a fancy solution for you but I can suggest a method I have outlined in my head.

 

1. Use a Record ID tool to "Sort" your records as they are now.

2. Use a Formula tool and use the formula rand(). There are quite a few significant figures so hopefully there are no duplicate values (odds are low). You can use a unique tool as a checker for this.

3. Use a Sort tool and rank the field which has the random numbers. 

4. Use a Multi-Row formula to rank the records in this new order.

5. Use another Sort tool to sort by Record ID to return your records to the order they were in previously.

 

I have attached my workflow below for you.

 

- Andrew

danilang
19 - Altair
19 - Altair

Hi @ts22 

 

What do you want use the random numbers for?  Why is it important not to have duplicates?

 

Is it to apply a random sort order to your data?  if so then create a new field with Rand() as the formula and sort by this new field.  

 

Is it to select a random subset of the data? In that case use the Sample tool.

 

Dan

bb213
8 - Asteroid

@danilang 

 

One use case would be if you were creating a Personal ID number for employees. 

bb213
8 - Asteroid

probably not since that goes against the definition of "random". 

JAKupchik
6 - Meteoroid

I think this post is very smart.  I'll be applying something of this sort to a Scrabble optimizer, for which I am determined to make a unique solution.

Labels