Thread: Help~~how to display a random sequence

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    8

    Help~~how to display a random sequence

    How to display a random sequence numbers in the range
    1 - 1000 , every number should be displayed once and no repetition~~~
    thx~~~

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    For one idea, use an array saying which ones you've used already.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The simplest way to go about this (a random range with no duplicates) when the range is relatively small is to randomly shuffle an array initialized to [1..N). A naive implementation might look like this:
    Code:
    for (i = 0; i < n - 1; i++)
      swap(&list[i], &list[i + ( rand() % (n - i) )]);
    Other solutions using an array are very inefficient, requiring you to save and search the array to see if a number has already been displayed. If the range is very large then an array really isn't the right data structure.
    My best code is written with the delete key.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Prelude
    Other solutions using an array are very inefficient, requiring you to save and search the array to see if a number has already been displayed. If the range is very large then an array really isn't the right data structure.
    Unefficient, but fun, and easily understandable code.
    Code:
    #define lOOO 1000
    #define l000 0
    #define l00O 1
    int lOO0[lOOO]={l000};
    int lO00=l000;
    int l0O0=l000;
    while( lO00 < lOOO-l00O )
    {
        if(lOO0[l0O0=rand()%lOOO]++==l000)
        {
            printf("%5d", l0O0 );
            lO00++;
        }
    }


    Quzah.
    Last edited by quzah; 12-04-2004 at 03:32 PM. Reason: Made it more readable. ;)
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    You just love defines dont you quzah??

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes. Yes I do.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    8
    thx to everyone~~~

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    8
    Quote Originally Posted by quzah
    Unefficient, but fun, and easily understandable code.
    Code:
    #define lOOO 1000
    #define l000 0
    #define l00O 1
    int lOO0[lOOO]={l000};
    int lO00=l000;
    int l0O0=l000;
    while( lO00 < lOOO-l00O )
    {
        if(lOO0[l0O0=rand()%lOOO]++==l000)
        {
            printf("%5d", l0O0 );
            lO00++;
        }
    }


    Quzah.
    interesting~~i like it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 08:09 PM
  2. Making a random array of integers
    By Vidak in forum C# Programming
    Replies: 2
    Last Post: 11-09-2007, 06:00 AM
  3. random picture
    By sniped22 in forum Windows Programming
    Replies: 10
    Last Post: 07-22-2007, 07:54 PM
  4. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  5. time or display problem I am not sure!
    By hamsteroid in forum C Programming
    Replies: 3
    Last Post: 04-09-2007, 01:52 PM