Better way to use Workflow.sideEffect API to generate multiple random integers

Hi,

Apologies if the below question has already been answered.

I need to generate n random integers for one of my use cases. I am trying to find the right/ better way to do that. I have the following 2 implementations in my mind -

Implementation 1

Random rand = new Random();
    List<Integer> list = IntStream.range(0, n).mapToObj(i -> Workflow.sideEffect(Integer.class, rand::nextInt)).toList();

Implementation 2

Random rand = new Random();
    List<Integer> list = Workflow.sideEffect(List.class, IntStream.range(0, n).mapToObj(rand::nextInt)::toList);

Which one is better and why?

The better way in Java is to use the Workflow.newRandom instead of the SideEffect.

Thanks for the response @maxim. A follow-up would be the same question but do something random. How would you respond to that? e.g. generate a random identifier with some internal implementation.

Use (2) as it adds a single marker instead of n to the event history.

1 Like