2

Is it possible to get the seed from a Random() sequence in c#?

My goal is to create a kind of editor, where the player creates his character, like he'd be able to choose in a set of different eyes, place them, then hair, etc. In the end it would output an array with a sequence of different numbers.

With that I want to get the seed because my goal is to be able to then share this character with other people. I would just give them the seed and they would have the exact character I created.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Heatith
  • 23
  • 2
  • +1 for writing "what I really want to achieve" part of the question, as it turned out you need something totally different from the question you've asked (quite common, and impossible to detect without "want to achieve" part). – Alexei Levenkov May 31 '12 at 21:04
  • Thanks for having edited my message. In my mind it was clear though haha. But now that I've reread it, it's true that the question is not well structured and completely differs from what I wanted to achieve. This website seems to have members that can decode newbie's minds, it's cool (won't bother you again with poorly asked questions anymore though). Have a great day everybody, and thanks again. – Heatith May 31 '12 at 21:12

5 Answers5

4

Based on your comments to @SLaks I think the question you're asking has nothing to do with the problem you're trying to solve.

The problem you're trying to solve is: How do I take a set of user-defined attributes and values, and turn them into something easily passable between friends?

This is exactly the same problem has: How do I take a long, complicated URL and turn it into a short URL?

As such, I would look at this famous question re: URL shorteners: How to code a URL shortener?

There you will see the use of Bijective Functions and a great algorithmic description that you can use for your own problem. This can be used to turn your example sequence (say 23,56,45,78,80) into a one or two letter code.

Community
  • 1
  • 1
yamen
  • 15,390
  • 3
  • 42
  • 52
  • Thanks so much! That's exactly what I was looking for. I simply didn't express myself very well (probably because I'm not a native english speaker and have a messy mind). – Heatith May 31 '12 at 20:46
  • There is still a lot of work for you to do in order to work out exactly how to encode your attributes. What remains the simplest is to store the users attributes in a table, encode the row id using the algorithm, and give them that to share. – yamen May 31 '12 at 20:48
  • Ok, I will take my time and try to do it well. :) But now at least I know where to head. – Heatith May 31 '12 at 20:54
3

There isn't a way to directly back out the seed from an existing Random class instance, or the sequence generated. However, when you create the Random class, you can use the constructor that allows you to specify the seed. If you then save this seed, you could pass it to another person.

If you want your construction to be random, but save that seed, one option is to use 2 instances of Random. The first could be used to create a seed, and then build a second with that seed number (which you'd save and pass to the other person).

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Thanks for your answers guys, I appreciate. I already know that, what I wanted to know is that for example, if a player creates his own sequence, it can output a seed that will then be used by Random(seed) to output the very same sequence the player created when creating the character. Does that make sense? – Heatith May 31 '12 at 20:31
  • @Heatith See my second paragraph - you could just randomly generate the seed, and then have them save that seed and send it to other people. – Reed Copsey May 31 '12 at 20:33
  • The thing is that I don't want to generate it randomly. I just thought that if a user specifies his own sequence, it could output a seed that could then be used by Random(seed) that would output the same sequence. But it's merely impossible. I'm dumb. Thanks for your quick answers though! I should have learned more about it before asking stupid questions. It's a mistake I won't make anymore, sorry. – Heatith May 31 '12 at 20:44
  • @Heatith Yeah - that's not possible. You'd have to make it randomly generate for that to work. – Reed Copsey May 31 '12 at 20:49
  • +1 to answer for question written, but turned out OP wanted something completely different form "what I want to achieve" part of the question. – Alexei Levenkov May 31 '12 at 21:02
2

You should just store the seed when you first create it.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • I may not have explained myself properly. My code, from a seed (so Random(seed)) will output a sequence (say 23,56,45,78,80) and will therefore create a character based on this sequence. But I also want the player, in an editor, to create his own sequence, and from it, get a seed, that would be used by others to output the same sequence, so the same character – Heatith May 31 '12 at 20:26
  • Sorry i edited, i pressed "send" before finishing my post. (writing from ipad is not easy) – Heatith May 31 '12 at 20:28
2

Why don't you just store the character's attributes, instead of the seed? It's straightforward and makes a lot of sense.

Besides, if the user creates his own sequence, it's not going to have "a seed".

zmbq
  • 38,013
  • 14
  • 101
  • 171
  • Hm yeah I'm stupid, as the user is going to specify his own sequence, and Random() uses very specific calculations, they will never be equal. – Heatith May 31 '12 at 20:38
0

Another approach is to give user choice of pre-generated sets of characteristics (i.e. based on random sequence with known seeds). This way you will need to just store small seed for you sequence (potentially using custom pseudo-random generator).

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179