d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Randomization Problem > C#
12Next
Add Reply New Topic New Poll
Member
Posts: 129
Joined: Dec 7 2012
Gold: 0.00
Dec 9 2012 10:20am
Code

private string ReadTextFile(string path)
       {
           if (path != "")
           {
               string[] text = File.ReadAllLines(path);
               Random r = new Random();
               return text[r.Next(0, text.Length)];
           }
           else
           {
               return "";
           }
       }


So i have this code, ran 3 times on 3 differents files of 3 lines each. My problem is that it keeps returning the same lines for each file.

Ex : If line 1 is returned from file 1, it'll return line 1 form file 2 and file 3.

Any idea?
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Dec 9 2012 10:51am
not sure what language thats in (i think its C#) but anyways it sounds like you are not seeding a random value.

in C its done as so.

Code
srand(time(NULL)); //or any other value you wish to seed but time is most comon.
int random = rand();


also with such small choice of lines you have about a 33.333~ chance of getting the same line. which is pretty great so you may have to run your application a couple times to see changes, but i do believe its because of the lack of seeding.

This post was edited by AbDuCt on Dec 9 2012 10:53am
Member
Posts: 129
Joined: Dec 7 2012
Gold: 0.00
Dec 9 2012 11:04am
Yup it's C#. I've never had problems with the Random class before :0
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Dec 9 2012 11:48am
Quote (Kalei @ Dec 9 2012 01:04pm)
Yup it's C#. I've never had problems with the Random class before :0


yea idk i dont have visual studios where i am atm so i cant help much.

your implementation looks correct. have you tried to set a breakpoint inside the function and examine that each variable holds at each line? what does text.length return? i assume its returning the LENGTH of the string at text[0].

i think the method you want to be using is .count

http://msdn.microsoft.com/en-us/library/system.string.aspx

Code
private string ReadTextFile(string path)
      {
          if (path != "")
          {
              string[] text = File.ReadAllLines(path);
              Random r = new Random();
              return text[r.Next(0, text.Count)];
          }
          else
          {
              return "";
          }
      }


string.Count returns the total number of elements in a string array. which is what i think you intended (because each string is in its own element).

edit:: also you dont need that else clause there. just do this.

Code
private string ReadTextFile(string path)
      {
          if (path != "")
          {
              string[] text = File.ReadAllLines(path);
              Random r = new Random();
              return text[r.Next(0, text.Count)];
          }

          return "";
      }


if the if statement doesnt trigger it would return "" anyways. and if it does trigger it returns the text. does the exact same thing but removes like 4 lines.

This post was edited by AbDuCt on Dec 9 2012 11:51am
Member
Posts: 129
Joined: Dec 7 2012
Gold: 0.00
Dec 9 2012 11:50am
Lenght return the lenght of the array. I could compare that to sizeof(array)/sizeof(array_type) in C++
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Dec 9 2012 11:53am
Quote (Kalei @ Dec 9 2012 01:50pm)
Lenght return the lenght of the array. I could compare that to sizeof(array)/sizeof(array_type) in C++


you sure. msds claims

Quote
Length Gets the number of characters in the current String object.


and

Quote
Count<Char>() Overloaded. Returns the number of elements in a sequence. (Defined by Enumerable.)


i believe you would have to use .Count in order to get a random value to use as an element to return beecause .Length returns the number of characters inside the current element.

ex:

text[0] = "hai"
text[1] = " bai"

text.Length() would return 3

and text.Count would return 2

because text (aka text[0]... text is a pointer to the first element of the string array) has 3 characters in it.

where as text.Countt returns the total number of elements in the array.

it might be returnng 1 for every file because of some weird segfault where you are trying to return a string element that doesnt exist.

as in if your function would pick 3 as the random value and try to return text[3]. it would segfault because there is no allocated memory at that address space.

at least i think thats whats happening here

This post was edited by AbDuCt on Dec 9 2012 11:58am
Member
Posts: 129
Joined: Dec 7 2012
Gold: 0.00
Dec 9 2012 11:58am
Problem solved. Declared r as a field of my form. Got him out of the function.
Member
Posts: 33,570
Joined: Mar 3 2007
Gold: 2,090.85
Dec 19 2012 06:47pm
The random class uses a seed (defaults to current datetime). I suspect that if this code was running fast enough then the same seed was injected resulting in consistent random results.
Member
Posts: 827
Joined: Jan 16 2012
Gold: 0.00
Warn: 10%
Dec 20 2012 07:31am
Quote (Xpander @ Dec 19 2012 10:47pm)
The random class uses a seed (defaults to current datetime). I suspect that if this code was running fast enough then the same seed was injected resulting in consistent random results.


This. Try using DateTime.Ticks method to seed the Random object.
Member
Posts: 3,064
Joined: Mar 30 2003
Gold: 5,095.00
Dec 28 2012 03:48pm
I didn't bother reading the other replies... so this was probably already said... but...

When you declare the random object... it creates a time seed... based on the current time... then when it generates a random number... it bases it off of that... so... if you declare to random objects beside each other... and call both of their "Next" functions... they will likely generate the same number.. HOWEVER... if you call the "Next" function twice on the same object... it will generate a new number based on the seed... so it will generate two different numbers.

TL;DR
Declare your Random object outside the function, and just call the Next() function on it.
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll