heal-research / SimSharp

Sim# is a .NET port of SimPy, process-based discrete event simulation framework
MIT License
126 stars 30 forks source link

Add method to randomly sample from a set of choices #26

Closed CrystalWindSnake closed 4 years ago

CrystalWindSnake commented 4 years ago

Is it possible to consider setting the attribute Random in the Simulation object to be public?

I try to add an extension method for object Simulation to implement the choics method like python.numpy.random.choice ,

But I found that the attribute Random cannot be obtained from the object Simulation

public class Simulation
{
     protected IRandom Random { get; set; }
}

so sad

abeham commented 4 years ago

Two possible approaches in my opinion:

  1. Move all Rand* methods from Simulation to IRandom
  2. Implement RandChoice(IEnumerable options) and RandChoice(IRandom random, IEnumerable options) as part of Simulation rather than in an extension method

Option 1 would break existing applications though as they would have to change from sim.RandUniform(a, b) to sim.Random.RandUniform(a, b).

If you want to make a PR for option 2?

CrystalWindSnake commented 4 years ago

Thank you for your reply.

For option 1, I feel that break existing applications is not good. For option 2, all Rand* methods must be provided by the library, users cannot implement their own extensions,may need a bit more scalability in Rand methods

option 2 looks better. Is there any other better option?

abeham commented 4 years ago

@CrystalWindSnake there is always the option of subclassing Simulation and providing the Rand* extensions as part of the instance. Anyone can use such a subclass in their applications.

Another option would be to leave the methods in Simulation and implement them in IRandom as well whereas Rand* methods in Simulation then only forward the call to the protected IRandom instance. Then anyone could add methods to IRandom, still they'd need to define their own source of randomness and not use the default RNG of the Simulation class.