Random number generation without repeats

This is a discussion on Random number generation without repeats within the C Programming forums, part of the General Programming Boards category; Hello friends, Need help in generating random numbers from an array without repeats.Once a number is generated, the number has ...

  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
    22,308
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 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. 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, 04:35 AM

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