Curious issue with random not being all that random (c# windows universal app)

Mr G Reaper

New member
Apr 20, 2015
86
0
0
Visit site
ok so heres the issue, my app generates a lot of random content, i mean tons of the stuff. to avoid having it all sound to similier each part thats randomly generated has a lot of options.

so here is the issue im noticing when two items are created im getting a lot of matches.

heres an example, these two taverns were generated one after another :
The hungover skunk, The hungover skunk is adorned with paintings of all its patrons. It has a somewhat shiny sawdust floor.
the patrons don't really care about strangers and will often just ignore them.
the bard at The hungover skunk is often accompanied by a dancing walrus.
The bar keep is a Dwarf with bright grey hair and a pair of goggles on his voice is so high-pitched its hard not to laugh.

The drowned tiger, The drowned tiger is rather run down. It has a old sawdust floor.
outsiders often feel they are trespassing when in The drowned tiger they know they are not welcome.
a cat is often brought out for people to fuss if they ask for a bard, no one really knows why.
The bar keep is a Elf with long fingernails and a cross eyed expression her voice is so high-pitched its hard not to laugh.

see the last line? there is about 15 voices it could choose, not compelling evidence i now, here is the other two taverns the system generated (these would of been made a micro second after the first two, basicly two towns were generated and the taverns above were made when the first town was made and the second two made when the second town was made:

The angry Dwarf, The angry Dwarf is is unpleasantly decorated, Dwarf ears are nailed to the wall above the bar.. It has a bloodstained wood floor.
this is not a tavern for strangers and they are informed of this very fast, often while being propelled from the establishment.
in the corner is a dead bard, it is apparently not uncommon for badly performing bards to be left there.
The bar keep is a Halfling with a black eye and a large pony tail on the back of her head her voice is so high-pitched its hard not to laugh.

The broken dove, The broken dove is is unpleasantly decorated, mounted Dwarf heads are nailed to the wall above the bar.. It has a somewhat shiny tiled floor.
few strangers ever go to The broken dove even fewer ever leave.
in the corner is a dead bard, it is apparently not uncommon for badly performing bards to be left there.
The bar keep is a Dwarf with red glowing eyes and a bald head her voice is deep and gravely.

notice it picked the same coice again and the dead bard is duplicated (theres about 30 different lines that can go in that spot (some wich use other lines to for a near infinite varity)

I set up an instance of random when the class is first instanced with
Code:
public Random rnd = new Random() ;
then when ever i need a random number i do rnd.Next(MaxNumber) (ie rnd.Next(10) for a random number between 0 and 9)

any ideas on how to get a truely random number when doing rnd.Next ?


EDIT:
I googled a bit more and found that making a static class for random is considered better

the result:
The sleazy bear, The sleazy bear is is unpleasantly decorated, thumbs are nailed to the wall above the bar.. It has a surprisingly clean sawdust floor.
this is not a tavern for strangers and they are informed of this very fast, often while being propelled from the establishment.
bards are not only forbidden from the The sleazy bear but people that try to sing often find a sudden inability to continue, throwing axes are funny like that.
The bar keep is a Elf with a large axe on his back and the air of importance about his his voice has no emotion in it.

The hungry Human, The hungry Human is painted multiple colours, its rather unsettling. It has a old chequered floor.
you really do not want to be a stranger in this tavern.
bards are not only forbidden from the The hungry Human but people that try to sing often find a sudden inability to continue, throwing axes are funny like that.
The bar keep is a Elf with a single monocle over his right eye and dirty hands his voice is rather unremarkable.

The fearful camel, The fearful camel is decorated with rough stone slab walls. It has a stained chequered floor.
there is no real atmosphere as the place is often empty.
bards are not only forbidden from the The fearful camel but people that try to sing often find a sudden inability to continue, throwing axes are funny like that.
The bar keep is a Human with a cheerful expression and red glowing eyes his voice has a distinct accent to it, though from where is hard to pinpoint.

The sad little Dwarf, The sad little Dwarf is decorated with the pelts of locally hunted animals. It has a new marbled floor.
few strangers ever go to The sad little Dwarf even fewer ever leave.
there is a bard at The sad little Dwarf patrons often get him too drunk to sing, for reasons that are all too apparent when he is sober.
The bar keep is a Dwarf with long fingernails and a small pet kitten on his shoulder his voice sounds like it came from the grave itself.

The horny bird, The horny bird is decorated with rough stone slab walls. It has a bloodstained granite floor.
this is not a tavern for strangers and they are informed of this very fast, often while being propelled from the establishment.
the inn is too small for a bard.
The bar keep is a Elf with a small pet lion on his shoulder and the smell of roses about his his voice has a distinct accent to it, though from where is hard to pinpoint.

That seems much more random,
for those that may stumble on to this via google
the class is
Code:
public static class Sr
    {
        private static int seed;

        private static ThreadLocal<Random> threadLocal = new ThreadLocal<Random>
            (() => new Random(Interlocked.Increment(ref seed)));

        static Sr()
        {
            seed = Environment.TickCount;
        }

        public static Random Instance { get { return threadLocal.Value; } }
    }

and to call a random number you simply
Sr.Instance.Next(minNumber,maxNumber) ie Sr.Instance.Next(0,9) for random number between 0 and 9


anyone know a better way im all ears :)
 
Last edited:

a5cent

New member
Nov 3, 2011
6,622
0
0
Visit site
I could be wrong since I can't see all of your code, but I suspect you're creating a new instance of Random in a method, invoking Next() on it, and then repeating that sequence whenever you require a new "random" number. The problem is that by using the default constructor you're always implicitly providing the same seed value, and allowing for the same amount of time to pass until you invoke Next(), resulting in the same pseudo random number each time.

If I'm correct, then you'd be better served by creating a single instance of Random, providing the constructor with a time based seed, and always reusing that single instance whenever you require a random number.

Random random = new Random(int) DateTime.Now.Ticks & 0x0000FFFF);

That should fix your problem.
 

Mr G Reaper

New member
Apr 20, 2015
86
0
0
Visit site
random was instanced just the once at the start and then used through out but i did find that was not the best way so have now changed to the static class method.

the date idea would work but the amount of times i need a random number....its kind of the main thing of my app lol man that would be a lot of typing
 

a5cent

New member
Nov 3, 2011
6,622
0
0
Visit site
^ If by "date idea" you mean providing the seed value, then you do that but once for the entire program. I'm not sure why you think the amount of times you need a random number makes any difference.

Unless you're accessing the Random instance from multiple threads, the whole thread local approach seems like overkill to me. I'd just use the singleton pattern. More information here:

C# in Depth: Implementing the Singleton Pattern
 

Mr G Reaper

New member
Apr 20, 2015
86
0
0
Visit site
i apologize i miss read what you put, i thought you ment you use the date(seed) each time you require a new number (in my defence its 3 am here lol) have bookmarked the thread you linked and will read when more awake.
I dont mind over kill, as the app grows more and more details will be added, all of these rely on random generators, often multiple time within the same string, anything i can do to make the random result more random im happy with :)
 

a5cent

New member
Nov 3, 2011
6,622
0
0
Visit site
Using thread local storage doesn't contribute anything to getting "more random" numbers. Using a single instance, which is what your static class members achieve, together with a time based seed, already gets you numbers as random as you'll ever get.
Using thread local storage only makes sense if you're solving threading issues, otherwise it's suggesting your code is solving problems that don't actually need solving. No biggie if you're the only one using your code, but a big no-no if you're working in a group. Like Einstein said, make it as simple as possible, but not simpler ;-)
Good luck!
 
Last edited:

Members online

Forum statistics

Threads
323,252
Messages
2,243,524
Members
428,049
Latest member
velocityxs