help me with random selection

Discussion in 'C' started by tomoyo_13m, Feb 17, 2006.

  1. tomoyo_13m

    tomoyo_13m New Member

    Joined:
    Feb 17, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    i have a case study....
    my prof said that we should display 10 random numbers without repeating the same number for example the screen will print numbers from 0-9 at this order

    1 6 7 5 2 3 4 9 8 0

    then when we exit the program it should print another random number... plss help i needed it very badly


    ahm... my idea is having a 3 for loop the first loop will generate the 10 random numbers from 0-9 and the second loop will check if the number that was generated before is already in the array and it will check if the it is the last in the array,if it generate the second number and compare it to the first number and it check that it is the same it will generate again...i need a counter in this program but i dont know hoe to do the second loop im thingking of doing a while loop instead of for loop
     
  2. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    38
    Trophy Points:
    28
    This seems to be a very simple problem.

    Does your program needs random number between 0-9 or it can be any random no not being repeated.

    Also you dont need 3 loops but 2 will do using the break and continue statement. Here is the sample of what you are looking for probably
    Code:
    int i=0,j=0,r;
    int arr[10];
    int flag = 0;
    for(i=0;i<10;)
    {
    	r = rand();
    	if(r < 10)
    	{
    		flag = 0;
    		for(j=0;j<i;j++)
    		{
    			if(arr[j] == r)
    			{
    				flag = 1;
    				break;
    			}
    		}
    		if(flag == 1)
    			continue;
    		arr[i] = r;
    		printf("%d ",r);
    		i++;
    	}
    }
    printf("\n");
    
     
  3. tomoyo_13m

    tomoyo_13m New Member

    Joined:
    Feb 17, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    it needs to print 10 number from 0-9 without repeating.. not nessesarily 0-9 it can be 11-20 as long as it display 10 numbers without repeating
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    The above program by coderzone does exactly same. If you need the range to be between any definite set change the condition accordingly.

    coderzone:
    Its always good to help somebody with the program logic but its not good to put the complete program as a whole so that he does not need to anything but just copy and paste.
     
  5. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    38
    Trophy Points:
    28
    I agree but the problem is so small that just giving the hints resulted in the complete program.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice