Hey all. I think I have one of those questions that raises red flags about how I have implemented something. I created a generic interface, call it IMyInterface<T>, and now I want to create a collection of those interfaces, like IEnumerable<IMyInterface>. Obviously this is in error. Is there a better approach? Or just a keyword I am missing?
@Hexum064 you can nest generic type statements i.e. IEnumerable<IMyInterface<T>> but you do have to know what T is at the time you declare the collection
hmm. I didn't think so. And that makes me think my approach is wrong. My interface is really simple. The generic type is used to set the return type of a single method in the interface.
private long randomLong()
{
Random random = new Random();
byte[] bytes = new byte[1000];
random.NextBytes(bytes);
return BitConverter.ToInt64(bytes, 0);
}
why is this method generating a random negative long?
Why don't you just generate two random Int32 values and make one Int64 out of them?
long LongRandom(long min, long max, Random rand) {
long result = rand.Next((Int32)(min >> 32), (Int32)(max >> 32));
result = (result << 32);
result = result | (long)rand.Next((Int32)min, (Int32)max);
...
Oh you're initialising Random in the method, don't do that, if you call it in quick succession you'll very likely generate the same sequence several times
I can still recommend the accepted answer from the question, it seems well thought through... I cant vouch for it's behavior with negative numbers though, but two's complement is one that makes things intuitively work from time to time
Well, Rand.Next() always produces a positive number, and, if I remember my two's compliment right, concatenating two positive numbers gives you a positive number.