Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I want to generate a random number then to check if it is in the database. If not then redo it.

My pseudo code:

                        PIN = GenerateRandomNumber().ToString();

                    // check if it is existing in the database

                    var listID = list_from_database
                    if (!listID.Contains(PIN))
                        // Save To database
                    else
                    {
                         // redo it until listID contains PIN
                    }
share|improve this question

closed as off topic by Glenn Rogers, Jeff Vanzella, svick, dreza, Corbin Nov 11 '12 at 6:47

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

5  
This should really be on StackOverflow, Code Review is for working code - see the FAQ –  Trevor Pilley Nov 9 '12 at 20:34

1 Answer 1

up vote 1 down vote accepted

I don't have the ability to transfer this to SO so I'll give my answer:

Just use a loop instead of recursion.

while(true)
{
    PIN = GenerateRandomNumber().ToString();

    var listID = list_from_database

    if (!listID.Contains(PIN))
    {
        // Save To database

        break;
    }
}
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.