Thread: Random number generation without repeats

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    1

    Thumbs down Random number generation without repeats

    Hello friends,

    Need help in generating random numbers from an array without repeats.Once a number is generated, the number has to be deleted from the array so that only the remaining numbers can be generated from the array the next time we call that function.And it should go on until all the numbers from the array are deleted.


    The code i have developed is below

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <conio.h>
    
    int random ()
    {
        srand (time (NULL));
    
        int picked[8], i;
        for (i = 0; i < 9; i++)
             picked[i] = 0;
        
        int array[9];  
        int value;
         
        for (i = 0; i < 9; i++)
        {
            value = rand () % 9;
            if (picked[value])
                i--;  // already picked.  for-loop increments, so decrement here
            else
            {
                array[i] = value;
                picked[value] = 1; // hasn't been picked yet.  Assign to array,
                                    // flag as picked.
            }
         }
         
         // display
         for (i = 0; i < 9; i++)
            printf("Values in the array are %d\n", array[i]);   
           getch();
          return value; 
    }
    int main()
    {
         int s;
        s = random();
    
        printf("value=%d", s);
        getch();
        
    }
    Hope replies asap from you friends.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by kishore84
    Need help in generating random numbers from an array without repeats.Once a number is generated, the number has to be deleted from the array so that only the remaining numbers can be generated from the array the next time we call that function.And it should go on until all the numbers from the array are deleted.
    Shuffle the array. Keep a pointer or index to the first element. Every time the function is called, pick the current element and increment the pointer or index (which could be an argument, or a static variable, depending on how you want to do it).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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, 10:13 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05: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. non repeating random number generation?
    By gencor45 in forum C# Programming
    Replies: 1
    Last Post: 02-08-2005, 05:23 PM
  5. Random Number Generation
    By drdroid in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 08-02-2003, 03:35 AM