random numbers

This is a discussion on random numbers within the C Programming forums, part of the General Programming Boards category; I know that you can scale random numbers down to a range that you want. For instance I know that ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    43

    random numbers

    I know that you can scale random numbers down to a range that you want. For instance I know that I can generate a random number between [m,n] inclusive.
    What I want to do is generate a random number between [m,n], BUT I want to exclude certain numbers in this range. For example let's say that I wanted to generate a random number between 4 and 10,but I want to exclude 6 and 8. How could I do that? Is it just as simple as something like this:

    rand() !=6 && !=8;

    I know that the above code is not correct,but am I on the right track?

    As an addendum as I continue to generate random numbers I want to exclude the random numbers that I have generated. For example lets say that I generate 7, now I want to exclude 6,8,and 7
    Last edited by mesmer; 10-24-2008 at 01:19 AM.

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    No, you can't do it like that. You will need to store every random number you generated in an array or linked list and then discard a generated random number if it is in that list.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    Post

    Code:
    int excluded[] = {6,8};
    int genRand() {
        int myNumber = (rand() % (n+1)) + m;
        for (int i = 0; i < sizeof(excluded) / sizeof(excluded[0]); ++i)
            if (myNumber == excluded[i]) {
                myNumber = genRand();
                break;
            }
        return myNumber;
    }
    or something like that

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Perhaps you want a random shuffle of the allowed numbers.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    43
    Quote Originally Posted by C_ntua View Post
    Code:
    int excluded[] = {6,8};
    int genRand() {
        int myNumber = (rand() % (n+1)) + m;
        for (int i = 0; i < sizeof(excluded) / sizeof(excluded[0]); ++i)
            if (myNumber == excluded[i]) {
                myNumber = genRand();
                break;
            }
        return myNumber;
    }
    or something like that

    Thanks for the help, have not had time to try this out yet. Can't wait to get to my editor.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 09:47 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 06:51 PM
  4. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 09:14 PM

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