problem with random sorting

This is a discussion on problem with random sorting within the C Programming forums, part of the General Programming Boards category; I am doing an assignment where I am trying to get random numbers between 1-10. But I dont want any ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Location
    Bangladesh
    Posts
    7

    Question problem with random sorting

    I am doing an assignment where I am trying to get random numbers between 1-10.
    But I dont want any number to come more than one time. I am using the following code.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include<conio.h>
    #include <time.h>
    void randomsort ();
    
    int main ()
    
    {
       clrscr();
       randomsort();
       getchar();
       return 0;
    
    }
    
    void randomsort()
    
    {
    
    
      int b[10],i;
      time_t t;
      srand ((unsigned) time(&t));
      for(i=1;i<=10;i++)
      {
      b[i]= rand() % 11;
      if (b[i]==b[i])
      printf ("%d\n",b[i]);
      }
    }
    It chooses random numbers but the output is like
    0
    2
    0
    2
    2
    1
    1
    9
    9
    It is giving me the number more than ones.
    How can I solve this problem? Please help.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,420
    Fill the array with the integers from 1 to 10. Shuffle the array, and you're done.

    To shuffle the array, first select at random one of the array elements, then swap it with the first element. Then, consider the subarray consisting of the array elements other than the first element. Select at random one of the elements from this subarray, then swap it with the first element of this subarray. Consider the subarray consisting of the subarray's elements other than the first... repeat until the subarray is one size 1.
    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

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    You've got a couple other issues here. . . index loops should go from 0 to < len (not 1 < len + 1). Also, if you want random numbers from 1 - 10, you should mod your rand() at 10 (giving you 0 - 9) then add one.

    As for your issue of random w/out replacement, I'd suggest you assign a tmp variable and, each time you get a new number, make sure it is not already in the list. OR, a more simple way to handle this is to create an array of int with each value being 1 .. 10, then mix up the list by having the random number generated be the new index of the value. This would, I think, properly mix up the data.

    EDIT: Crap, too slow on the type this morning. . . I must not be using "proper touch type" methods.

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Quote Originally Posted by Kennedy View Post
    You've got a couple other issues here. . . index loops should go from 0 to < len (not 1 < len + 1). Also, if you want random numbers from 1 - 10, you should mod your rand() at 10 (giving you 0 - 9) then add one.

    As for your issue of random w/out replacement, I'd suggest you assign a tmp variable and, each time you get a new number, make sure it is not already in the list. OR, a more simple way to handle this is to create an array of int with each value being 1 .. 10, then mix up the list by having the random number generated be the new index of the value. This would, I think, properly mix up the data.

    EDIT: Crap, too slow on the type this morning. . . I must not be using "proper touch type" methods.

    Another way do accomplish this, as I did something somewhat similar is to just populate an array element with '1' only if that random number(the index of the array) was hit. In other words use the index of the array as your deciding factor, have the complete array initialized to all 0's then use the random number you get by your generator as the index, then check its value, if set to 0, then change it to one, if there is a 1 already present then ignore and try again. Hope that makes sense.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,420
    Quote Originally Posted by slingerland3g
    Another way do accomplish this, as I did something somewhat similar is to just populate an array element with '1' only if that random number(the index of the array) was hit. In other words use the index of the array as your deciding factor, have the complete array initialized to all 0's then use the random number you get by your generator as the index, then check its value, if set to 0, then change it to one, if there is a 1 already present then ignore and try again.
    In this case, since it appears that Exorcist wants an array whose size is as large as the range of possible numbers, that may not be as efficient as normal shuffling since the chance of producing a duplicate would be high towards the end. (But of course, this kind of efficiency does not matter when you only have ten elements )
    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

  6. #6
    Registered User
    Join Date
    Dec 2009
    Location
    Bangladesh
    Posts
    7
    Thanx. I have managed to solve the problem. Thanx a lot for your help everyone

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with random number generator
    By Niss in forum C Programming
    Replies: 6
    Last Post: 10-01-2008, 06:03 PM
  2. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 02:08 PM
  3. Array Sorting problem
    By ___________ in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 12:17 AM
  4. Random numbers problem
    By ManiacBR in forum C++ Programming
    Replies: 4
    Last Post: 11-09-2006, 11:34 AM
  5. Random word problem
    By goron350 in forum C++ Programming
    Replies: 2
    Last Post: 05-14-2005, 03:44 PM

Tags for this Thread


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