question

Tom S4 avatar image
0 Likes"
Tom S4 asked Jason Lightfoot commented

Custom stream in RAND() queries

In researching model repeatability, I came across this answer which explains the importance of using random streams unique to each object: https://answers.flexsim.com/questions/22132/changes-in-breakdown-time.html

However, it appears the default behavior of using RAND() in queries (e.g., querying tables, lists, and storage slots) uses the same stream with no option to customize/set unique streams based on objects. It appears that it uses stream 0: https://answers.flexsim.com/questions/97304/sql-rand-keword.html

I don't have the best grasp of this concept yet, but I'm wondering if this behavior go against best practices? Are there workarounds that would be considered best practice? Are there plans to allow for custom streams in SQL queries?

FlexSim 25.0.0
random streamsrepeat random streamssql rand
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

Jason Lightfoot avatar image
1 Like"
Jason Lightfoot answered Jason Lightfoot commented

Hi Tom, two workarounds come to mind:

  1. With lists you can use a dynamic expression to generate a random number using a stream and sort on that field
  2. If you're trying to select a number of values at random then get the array of all entries and then choose one on the list at random.

Both of those techniques can specify the stream to use and in the second case avoid the unnecessary overhead of performing an expensive sort on a (potentially large, already ordered) list.


· 2
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Tom S4 avatar image Tom S4 commented ·
Hi Jason. Thanks for the quick response. Adding a dynamic expression to the list is a great idea, thank you.


For the second suggestion, specifically for queries storage slots, the "Find Slot" activity shouldn't be used in this case - this should be replaced with a custom code activity that uses querySlots() then select one at random i.e.,

// instead of Find Slot activity query,
`ORDER BY RAND()`

// use this:
Array slots = Storage.system.querySlots("");
Storage.Slot slot = slots[duniform(1, slots.length, getstream(activity)];

Correct?

0 Likes 0 ·
Jason Lightfoot avatar image Jason Lightfoot ♦♦ Tom S4 commented ·
Perfect!
1 Like 1 ·