Coder Social home page Coder Social logo

Comments (8)

aliostad avatar aliostad commented on August 23, 2024

Sorry do not quite get what you mean by "I require a single random item at a time".

Also I personally do not have a problem with ()().

But I do have a couple of problems with this:

  • this can lead to confusion and on the fact how to use it
  • people can simply use this overload over the function which will be wrong since we have an instance of Random as a closure in the Func while this will lead to creating the random object all the time.
  • In fact designing this as a Func was to ensure every func gets its own random object so the data are as random as System.Random can provide.

I feel this is pretty much done and don't think tinkering more with it could be useful.

from randomgen.

jkonecki avatar jkonecki commented on August 23, 2024

Regarding bullet points 2 and 3:

The closure of Random and the fact that a new instance of Random was created for every Func was actually an issue - I've fixed it by reusing the single instance of Random for every Func.

The problem was related to the fact that the default constructor of Random seeds the algorithm with the system clock dependent value (clock ticks) and creating multiple instances of Random one after the other would cause all of them to generate the same series of numbers.

On most Windows systems, Random objects created within 15 milliseconds of one another are likely to have identical seed values. (http://msdn.microsoft.com/en-us/library/system.random(v=vs.110).aspx)

I do understand that you're fine with ()().

What I mean by "I require a single random item at a time" is that I need only 1 random item at a time, not the whole series of them. As an example think of a message handler that needs to get a single random country:

public void Handle(Foo message)
{
    message.Country = Gen.Random.Countries()();
}

I personally would rather write:

public void Handle(Foo message)
{
    message.Country = Gen.Random.Country();
}

The above is a matter of personal preferences but also IMHO the principle of the least surprise (http://www.faqs.org/docs/artu/ch11s01.html). I can imagine that ()() construct may be confusing for some on first encounter.

On the other hand I don't think that the following two methods can be confusing due to singular / plural naming and different return types. They read very 'English' and natural to me.

Func<string> Gen.Random.Countries()
string Gen.Random.Country()

from randomgen.

aliostad avatar aliostad commented on August 23, 2024

No, it was correct - why have you changed it to static?!

Please note that the creation of Random object is outside the closure:

var random = new Random();
return () => (random.NextDouble() < 0.5);

from randomgen.

jkonecki avatar jkonecki commented on August 23, 2024

Yes, but calling two Func methods one after other would create 2 Randoms with the same seed:

var integers  = Gen.Random.Numbers.Integers();
var doubles = Gen.Random.Numbers.Doubles();

Also, MSDN documentation has the following section regarding multiple initialisations:

Avoiding multiple instantiations

Initializing two random number generators in a tight loop or in rapid succession creates two random
number generators that can produce identical sequences of random numbers. In most cases, this
is not the developer's intent and can lead to performance issues, because instantiating and
initializing a random number generator is a relatively expensive process.

Both to improve performance and to avoid inadvertently creating separate random number
generators that generate identical numeric sequences, we recommend that you create one
Random object to generate many random numbers over time, instead of creating new Random
objects to generate one random number.

from randomgen.

aliostad avatar aliostad commented on August 23, 2024

Exactly! That is why we return a factory not the random number itself. Random is not thread-safe and we should not use a single static Random object.

I think we can randomise the seed so we are both happy... how about that? Or actually basing it on RNG...

from randomgen.

aliostad avatar aliostad commented on August 23, 2024

One of the reasons I did not RNG to begin with was that it is a disposable resource and I did not want any of that headache.

So I think this should be pretty fine:

new Random(BitConverter.ToInt32(Guid.NewGuid().ToByteArray(), 10))

from randomgen.

jkonecki avatar jkonecki commented on August 23, 2024

I'll make the changes necessary.

I presume you're not interested in single item API?

from randomgen.

aliostad avatar aliostad commented on August 23, 2024

I think it is a good idead but honestly it can make it confusing on how to use the API. We should drive them to the pit of success 🎱

from randomgen.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.