c# - Get amount of serialized objects in stream to randomly choose one -
i'm trying amount of serialized objects in stream i'm serializing , deserializing from. want create random value between 1 , amount.
is possible , how?
currently code is:
random rnd = new random(); stream stream = new filestream("file.dat", filemode.open, fileaccess.read, fileshare.read); int randomnumber = rnd.next(1, (int)stream.length); however, won't work since stream.length value on 1000, whilst there 3 serialized objects in file.
i'm using binaryformatter serialization.
edit: should add objects serialized file multidimensional arrays (int[,]), , have number stored them. first object has number 1, second has 2, etc. in advance!
you can select item @ random list of unknown size, you'll have scan entire list in order it. not, however, have keep items in memory in order select one.
see article random selection large groups, heading "what if don't know how many there are."
Comments
Post a Comment