non repeating random number generation?

This is a discussion on non repeating random number generation? within the C# Programming forums, part of the General Programming Boards category; Any help would be appreciated! Im new to C# and im going crazy trying to figure out a simple way ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    1

    Exclamation non repeating random number generation?

    Any help would be appreciated!
    Im new to C# and im going crazy trying to figure out a simple way to create a 4-digit non-repeating random number for a number guessing game. If anyone can help with the code I already have I would be grateful.

    Heres what I have:

    Code:
    private void form1_Load(object sender, System.EventArgs e)
            {
    string answer;
                
    //Creates random number array
    
    String[] myNumbers={"0","1","2","3","4","5","6","7","8","9"};
    Random rndNumbers=new Random();
    I dont know what kind of loop make from here, and then I dont understand how to put the random number into a string

    Please help

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    I use something like this for my code i am using in my project -

    Code:
    Stack numberCheck = new Stack();
    int number = 0;
    Random randomNumber = new Random();
    bool exists = false;
    
    //generates a number then checks if it has already been made
    
    number = randomNumber.Next(1,100);
    exists = numberCheck.Contains(number);
    while(exists == true)
    {
    //shrink this code down later by making a function of your own
    //which can also be used above too
    number = randomNumber.Next(1,100);
    exists = numberCheck.Contains(number);
    }
    Should be fairly easy to see where you can change things to suit your needs.

    And use the Convert to change the int to a string ie. -

    string word = Convert.tostring(number);
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 11:13 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  3. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 05:16 PM
  4. random number between negative and positive number
    By anomaly in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2003, 08:40 AM
  5. Random Number Generation
    By drdroid in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 08-02-2003, 04:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21