As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I am trying to write a service in which every user will be assigned a unique name, when he first uses the service. I wish to generate this name, rather than getting the user to set it up. Also, I want the name to be somewhat readable and memorable rather than sound like a GUID or a timestamp. Essentially I want this to be something like the Xbox gamertag.
I know that there will never be more than a 1000 users so maintaining the uniqueness would not be a problem (another reason why I can afford to avoid GUIDs)
I am thinking of taking some adjectives, nouns etc. from the dictionary and generating random but unique combinations of those.
Any suggestions?
You could use a corpus of English language n-grams (say of three letter sequences) and use them to generate words that look like English, but are actually completely gibberish. This kind of data is essentially random, but has a softness for the nature of human language and memory.
This is similar to what I'm talking about, except it combines entire words into sentences probabilistically. I was thinking more of doing it by composing letter sequences into imaginary words.
EDIT actually this page discusses what I'm talking about.
This is just a code example to fully approach your problem. In case it doesn't solve it, please try to be more specific in your question. Pass to the following method an instance of the System.Random class and a list of words (your dictionary).
static string GetGuid(Random random, IList<string> words)
{
const int minGuidSize = 10;
const int maxGuidSize = 15;
var builder = new StringBuilder();
while (builder.Length < minGuidSize)
builder.Append(words[random.Next(words.Count)]);
return builder.ToString(0, Math.Min(builder.Length, maxGuidSize));
}
You can use this list of 10 000 random name:
http://www.opensourcecf.com/1/2009/05/10000-Random-Names-Database.cfm
or use this website to generate a random list of firstname:
http://www.fakenamegenerator.com/order.php
Safe way. maintain the list of remaining non used names.
Easy way (also very scalable) but unsafe. Rely on the unlikelyhood that 2 users randomly get the same id.
I would try to get 3 or 4 lists of about a thousand modalities and then randomly picking one value in each list. That would make about 10E12 possibilities which is enough to avoid collision for 1000 users.
JohnLampMartin2212
Related
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I'm working on a small self learning project with an EXP/Level calculator for a game and I just need a little advice.
I'm wondering what would be the best storage method to store a Level (1, 2, 3 up to 110) and an experience requirement for that level. I would like to be able to search for the level by an exp value, for example if level 9 is 1000 XP, and level 10 is 2,000 XP I'd be able to search for level 9 by inputting an XP value of 1000 - 1999.
So far I've researched SQL/List/Dictionary/Arrays and all seem good, but I can't find something that performs the operation I described.
Since the number of levels is only up to 110, you could just keep the ranges in an ordered list or array like so:
0
100
200
400
600
800
1000
1300
1600
and so on (I made up those numbers of course!)
Then you could search for the level for a particular XP value by either linearly searching the list (which would be fast enough probably) or if you really needed to you could use Array.BinarySearch() to find the nearest level, and do a check of the values to either side to see which band the XP falls into.
The level will just be the index of the found band + 1 (since levels start at 1, not 0).
To linearly search for the level for an XP value:
int[] xpArray = new [] { 0, 100, ... etc }// The list as shown above
int targetXP = 850;
int level = 0;
while ((level < xpArray.Length) && (targetXP > xpArray[level]))
++level;
// Now you have the right level.
Obviously this is just a brief overview to get you started! Also, you might need to adjust things slightly; I don't know your exact requirements. Does a character with 0 XP have level 0, for example?
The key thing to take away from this is the concept of searching a list of numbers, and using the index of the band that you find as a basis for the level, and also the fact that you only need to store one number for each band. You don't need to store a min and max value for each band, nor do you need to store the level. Those values are implicit.
I leave using Array.BinarySearch() to speed things up as an excercise for the reader... ;)
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
I know they are the same variable type, but is there an accepted standard 'style' for whether to use long or Int64?
I would like to use the most common one.
All documentation from Microsoft and 99.999% (or so) of the code you'll find online will use long. See for instance the numeric datatype definition in the C# reference: http://msdn.microsoft.com/en-us/library/exx3b86w.aspx
In the end, this is the same issue with using string or String. In general, lowercase names are used for value datatypes like numbers or characters, and uppercase names are used for classes. In C# Int64 is the complex datatype (a structure with fields, properties and methods, see the doc here) for the long value datatype (see the doc here). In general, people don't use the class Int64 for other than invoking methods from that class, such as Int64.Parse. So you will usually find something like this:
long variable = 9.5f;
string text = "8.4";
long anotherVariable = Int64.Parse(text);
You will get so many different opinions for a question like this since they're the exact same thing. So it's up to you. Microsoft pretty much always uses long, int and short in their documentation and examples. It's simply an Alias in VB.NET and C#. So I guess it's better usage to use them that way.
long instead of Int64
int instead of Int32
short instead of Int16
C# uses these keywords like long, int string, as aliases for .NET-types.
int = System.Int32
long = System.Int64
string = System.String
The first argument to use these keywords is that it comes with the language, so use it when writing the language.
A second argument of using these keywords is that you do not have to put using System; above your codefile.
Another benefit could be that someone could create a new type called Int64 and put it somewhere in your namespace (I know... it would be crazy). If your older code uses the type Int64, your older code could stop functioning. If your older code was using long (an alias for System.Int64) than it would still work fine.
You should use long, int and short.
The one exception I know is in a function name; look at BitConverter.ToInt64.
The reason for that is that long is not defined in CIL while int64 is.
I think it comes down to clarity versus compatibility. Int16, Int32 and Int64 are more clear than short, int and long. You know just by looking at Int16 that it is 16 bits in length.
I think in most cases it will come down to personal preference, but if you are strictly talking "C#" without specifying the underlying platform, I'd think a bit differently. If you are developing to adhere to C# standards for portability across platforms and "future-proofing", "long" would be the best way to go as it is an actual construct in the language itself. "Int64" is an actual struct (System.Int64) in the framework.
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'm new to C# programming so I need to ask C# experts here, what techniques should I use for a quiz game application I'm planning to develop. I'd like my quiz app to be like this:
1.) What is the capital city of UK? a. London b. Washington D.C. c. Tokyo d. Manila
2.) What is the capital city of Russia? a. Bangkok b. Beijing c. Islamabad d. Moscow
ETC....
I want the questions to be randomly generated using Rand(). The questions should be randomly placed, not in the order I set up here, but still those 4 choices I declared are the only ones to display in the set of the quiz. If you have any links to tutorials please give me so I can study it. I really love to develop this app, but I don't have any clue to start this. Any help is truly appreciated. Thanks!
There are many different ways to go about doing this. The way to do it if you want the option of being able to easily maintain the questions and answers is to put them in an XML file. You could then use XMLDocument to load the questions and answers at runtime. You XML file would look something like this:
<?xml version="1.0" ?>
<quiz>
<question>
What is the capital city of Russia?
<answers>
<correctAnswer>Moscow</correctAnswer>
<wrongAnswer>Bangkok</wrongAnswer>
<wrongAnswer>Beijing</wrongAnswer>
<wrongAnswer>Islamabad</wrongAnswer>
</answers>
</question>
</quiz>
You can parse this in C# using XMLDocument.
First you should understand your problem. Just check your requirements and think of objects. You certainly have "question" and "answers". Each Question has 4 possible answers and only one is correct. So a first, very simple approach would look like that.
class Question
{
public string QuestionText{ get; set; }
public string AnswerA { get;set }
public string AnswerB { get;set }
public string AnswerC { get;set }
public string AnswerD { get;set }
}
This is a good start, but not perfect. You could now store the correct answer aswell inside this question object. But to use this new property to its fullest, it would make sense to make our answers a bit more dynamic.
class Question
{
public Question()
{
Answers = new string[4];
}
public string QuestionText{ get; set; }
public string[] Answers { get;set; }
public int CorrectAnswer {get;set; }
}
So with this small object we can now create all our questions like this:
var question = new Question();
question.QuestionText = "What color is snow?";
question.Answers[0] = "Red";
question.Answers[1] = "Yellow";
question.Answers[2] = "White";
question.Answers[3] = "Green";
question.CorrectAnswer = 2;
// ... more questions
var listOfQuestions = new List<Question>();
listOfQuestions.Add(question);
How to sort randomly is another topic which is not difficult to find here on SO.
I personaly like icemaninds idea, you can use his answer to improve my basic approach.
What is your intended data source and how big is it? If you can define the format of your data source, I would suggest having a text file in which each line has three or four fields, separated by some sort of delimiter. The fields would be a question, the correct answer, and either a list of characters indicating the categories into which the question and answer both belong, or a list of categories for the question and another for the answer.
To clarify the last point, in many multiple-choice tests, if one were to simply choose ten questions at random from a pool of 25, and then for each question print three random answers from the pool along with the correct answer, one may end up with a question like: "How many sides does a triangle have? (a) square (b) Euclid (c) three (d) rhombus". A COMPUTE! magazine article some decades back offered a multiple-choice quiz generator which solved this problem by what it called "discrimination"--attaching categories to questions and answers, and for each question only picking answers that were suitable for the question's category. I don't remember how that article did things, but would suggest for simplicity of coding and data entry that you identify categories of questions and answers, and pick a letter for each. For the above question, a reasonable category might be "written-out whole numbers less than thirteen", so if one arbitrarily decides to use the character "Q" for that, both the question and answer would have a category of "Q". In many cases, a single category for question and answer would suffice (I think that's how the COMPUTE! program worked, but in some cases one may need to allow something more sophisticated (e.g. for "A shape with four sides, and with pairs of opposite sides equal, is:", it may be reasonable to offer up "pentagon" as an option, but probably not "square", "rectangle", or "rhombus").
There are a few more issues to consider in the design of the data set, such as how it should handle the possibility that multiple questions may have the same answer, and whether answers should be listed in random order or consistent order (e.g. for "How many sides does a pentagon have", it may be nicer to list the answers as "(a) three (b) five (c) six (d) eight" than as "(a) eight (b) five (c) six (d) three").
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
My recollection from a past employer is that they distinguished between the two as follows:
Validation is the process of checking that the data is appropriate in a very basic sense; for example that data in a date field can be converted to a date, or that characters in a number field can be converted to a number of the appropriate type;
Verification is the process of checking the typed data against some other 'business' rules that you impose on your interface - for example that the Date Of Birth field indicates an applicant within a certain age range.
These memories do not tie in with the Wikipedia article on the subject, nor a BBC BiteSize Revision article.
So what is the consensus: Do people care what methods and processes are called when I am checking Xml inputs for example?
What am I doing when I:
Check that a date field contains characters that are convertible to a C# DateTime;
Check that the DateTime is in an appropriate date range to be stored in SQL Server;
Check that the Date Of Birth indicates a customer over 18 but under 65?
In my vocabulary, validation is checking whether the format of the data is correct, IE if a you're actually dealing with a correctly formatted date string . Verification is checking whether the date you got is actually the date you were expecting.
Ok, so I'll take this as an open invitation to musing...
I think the difference is very much like compile-time vs. runtime errors. Just like the compiler is able to tell that two variables a,b are of type double, and thus the expression a/b is valid, it is only during runtime a DivideByZeroException may be raised if b turns out to be 0.
So to complete the analogy, one can validate that a string looks like a credit card number ('compile time'), but can only verify that it corresponds to a valid card only if one tries to charge the credit card ('runtime') with an amount
Duh. So I guess I understand it pretty much like you old company does.
in terms of programming it makes no difference what you call it (validation or verification) but where you put the logic is important. usually all three rules you mentioned are known to be validations with first two points corresponding to UI validation and last point to business rule validation. we usually validate UI fields using dataannotations in our controller and Business rule validation is performed within business layer. But the bottom line from software point of view is; don't do an operation (save, edit) unless data is good (you call it valid or verified).
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'm looking for a formula interpreter that I can use in a C# application. It needs to be able to interpret a string like this:
max(1+2, 4) * x
I found Writing a fast formula interpreter (codeproject.com) which almost does what I need but it doesn't allow for functions with multiple parameters. I could probably add that functionality to it but I was just wondering if something like this already exists.
Thanks
A couple I've used in the past with no problems:
NCalc
Fast Lightweight Expression Evaluator
You can actually build a very effective interpreter by parsing and replacing certain functional keywords such as max with Math.Max and then dynamically building and executing the formula as a C# class and method. So actually you would be parsing and wrapping the formula and allowing the C# compiler to interpret and execute it.
So, taking your sample formula of max(1+2, 4) * x would turn into:
public class MyFormula
{
public double calc(double x)
{
return Math.Max(1+2, 4) * x;
}
}
Which you would compile on the fly and then execute per the linked article. You still have to parse for and pass the x value of course.
A long time ago in one project i had to create some booking with formulas, and i used VsaEngine. To use this engine you need to add reference to Microsoft.JScript. Here is example:
Code for usage is very simple, just replace formula parameters like:
string formula = "x+y";
formula= formula.Replace("x","100").Replace("y","200");
string result = CalculateFormula(formula);
And here is core method, CalculateFormula:
public string CalculateFormula(string evaluationString)
{
VsaEngine en = VsaEngine.CreateEngine();
Object result = Eval.JScriptEvaluate(evaluationString, en);
return result.ToString();
}
With this you can create your custom formula interpreter engine.