OCLOperators random
No edit summary
(Replacing message template with parser tag)
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<message>Write the content here to display this box</message>
The random operator is used to generate a random value within a specified range or collection. It takes one or two arguments: the first specifies the lower bound or collection, and the second (optional) argument specifies the upper bound. The return value is a randomly selected element from the range or collection.
Int32.Random(Int32 x) returns a random integer [0..x-1].
Int32.Random(Int32 x) returns a random integer [0..x-1].


For example, Int32.Random(100) will return 0, 1, ...,99 - it will never return 100.
For example, Int32.Random(100) will return 0, 1, ...,99 - it will never return 100.


The example below shows one way to generate a random string with a random length (between 7-11 characters).
The example below shows one way to generate a random string with a random length (between 7-11 characters):


<pre>
<pre>
Line 10: Line 13:
     Sequence{1..wordlength}->collect(a|characters->at(Int32.Random(100).Mod(tecken->size)+1))->asSeparatedList( '')
     Sequence{1..wordlength}->collect(a|characters->at(Int32.Random(100).Mod(tecken->size)+1))->asSeparatedList( '')
   )
   )
)
)</pre>'''Note:''' the exact algorithm for generating random values may vary depending on the implementation of the operator. Also note that there is no random function on Int64
</pre>
[[Category:OCL General Operators]]
[[Category:OCL General Operators]]
{{Edited|July|12|2024}}

Latest revision as of 07:51, 17 June 2024

The random operator is used to generate a random value within a specified range or collection. It takes one or two arguments: the first specifies the lower bound or collection, and the second (optional) argument specifies the upper bound. The return value is a randomly selected element from the range or collection.

Int32.Random(Int32 x) returns a random integer [0..x-1].

For example, Int32.Random(100) will return 0, 1, ...,99 - it will never return 100.

The example below shows one way to generate a random string with a random length (between 7-11 characters):

let characters = 'ABCDEFGHJKLMNPQRST23456789'.toCharArray in (
  let wordlength = Int32.Random(5)+7 in (
    Sequence{1..wordlength}->collect(a|characters->at(Int32.Random(100).Mod(tecken->size)+1))->asSeparatedList( '')
  )
)

Note: the exact algorithm for generating random values may vary depending on the implementation of the operator. Also note that there is no random function on Int64

This page was edited 94 days ago on 06/17/2024. What links here