« first day (3602 days earlier)      last day (1361 days later) » 
04:00 - 20:0021:00 - 23:00

9:00 PM
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
 
I should have mentioned in my question that I won't know T. In fact, T would be different types for different implementations of the interface.
 
T in IEnumerable<T> and T in MyInterface<T> are different Ts but that doesn't matter since you have to specify both Ts when you declare anyway
Yeah you can't do that
 
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.
 
Well consider if I called myList[n].MyMethod() - what would its return type be?
 
9:09 PM
That's exactly what I was thinking about when I was typing in IEnumerable<>.
It seems I do shoot myself in the foot a lot when it comes to generics.
 
9:25 PM
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?
 
it shouldn't always be negative
 
I mean i'd like to generate a random positive long
and i've teste it out and it is always negative
i mean the four attempts were negatives
@TomW and i wish it could always be positive
 
it'll be the two's complement bit, and 4 in a row is not that uncommon but checking...
 
89
A: Random number in long range, is this the way?

DypplWhy 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); ...

looks promising
 
@Wietlol i've copied that method from one of the answers those with 2 positive votes
 
9:29 PM
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
 
Yeah, what Tom said
 
I've tested it and get a distribution of different positive and negative longs
 
@Riccardo why not the one I linked?
 
but if you feed it a random load of bytes you'll set the high bit 50% of the time
 
@Wietlol in that question there is an answer which has a method without parameters that's why
 
9:31 PM
use UInt64 instead BUT you can't cast that to long because it stands a chance of overflow
 
@TomW i've just put random outside the method
 
(the 1000 bytes also seems a bit overkill)
 
my approach would be something like
return rand.Next() | ((long)rand.Next() << 32);
 
@Grace could you please text a method as example
 
like, that's the entire body of the method
 
9:33 PM
@Riccardo but you dont just want no arguments
you want a minimum value of 0
or perhaps 1
considering 0 is not positive
 
private static Random random = new Random();

private long RandomLong()
{
return rand.Next() | ((long)rand.Next() << 32);
}
 
i don't want no arguments
@Grace i'm gonna test it out
thanks a lot
 
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
 
@Grace it works thank you so much
 
What can I say? I'm literally always right.
 
9:38 PM
how will that prevent negatives?
 
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.
 
hmm... that is true
 
if you really wanted to be sure, you could replace all mentions of long with ulong in there
 
then you dont have a long though
I suppose the easiest way would be to take an 8 byte array, fill it with randoms, convert to long and bitmask to non-negative
it would have an equal distribution of all non-negative possibilities
 
I mean, you could do the same thing with, like
long rand = rand.Next() | ((long)rand.Next() << 32);
if (rand < 0) { rand = -rand }
return rand;
or even

return Math.Abs(rand.Next() | ((long)rand.Next() << 32));
but I thiiink that's unnecessary
 
9:45 PM
looking at Java's implementation, they have... either your solution... or a very difficult one
@Grace you shouldnt use -num nor abs to avoid negatives
@Wietbot evalcsharp -Int32.MinValue
 
Microsoft.CodeAnalysis.Scripting.CompilationErrorException((1,1): error CS0220: The operation overflows at compile time in checked mode)
 
if you dont care about the exact conversion of the numbers, a bitmask on the two's complement should be fine
 
as a test, I ran

for (int i = 0; i < 5000000; i++) { if (RandomLong() < 0) { throw new Exception("I got a negative number over here."); } }

without Math.abs() or anything like that. No exception.
This isn't perfect, of course, but it's pretty suggestive to me
 
while (true) {
    val rnd = nextLong()
    if (rnd in from until until) return rnd
}
the heck is dis heresy?
val n = until - from
if (n > 0) { /*normal shit*/ }
else { /*!!!heresy!!!*/ }
and until > from
hmm...
this is totally weird, but everything is explainable afaik
 
 
1 hour later…
10:58 PM
 
04:00 - 20:0021:00 - 23:00

« first day (3602 days earlier)      last day (1361 days later) »