Thread: How get 10 random number

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    17

    How get 10 random number

    i have 10 object (a[10]) that they have int x and int y in their class.
    now i want get number for x and y with rand()(min=1 and max=15)
    and now each object have a position like (3,7)

    i need a function (or two) that find same position and if finded give new position and then check for same position again

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    class chess{
    
    public :
    	chess();  //constructor
    private:
    	int x;
    	int y;
    };
    
    main()
    {
    
    chess a[10];
    
    }
    
    chess::chess()  //get position
    {
    x=rand()%15+1; //rand()%max+min will give values from min to min+max-1
    y=rand()%15+1;
    }
    Last edited by aminpost; 02-25-2009 at 10:19 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    a[9] is a 9 element array!

    Aside from that, you probably have to post your code for us to tell you what is going wrong.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    17
    Quote Originally Posted by matsp View Post
    a[9] is a 9 element array!

    Mats
    a[0]-a[1]-a[2]-...-a[9]=10 element array (you do not count a[0])

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Don't ask for help and then argue with the help you receive. It makes you look foolish.

    int a[9]; // 9 element array
    int b[10]; // 10 element array
    b[9]; // access 10th element of 10 element array

    Soma

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And if you actually want help with your random number problem, you still need to post the code, regardless what you think your arrays should be declared as.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    17
    first say sorry you are right

    i need a function that find same position and if finded give new position and then check for same position again

    Code:
    #include <iostream.h>
    #include <time.h>
    #include <stdlib.h>
    
    class chess{
    
    public :
    	chess();  //constructor
    private:
    	int x;
    	int y;
    };
    
    main()
    {
    srand((unsigned)time(NULL));
    chess a[10];
    
    }
    
    chess::chess()  //get position
    {
    x=rand()%15+1; //rand()%max+min will give values from min to min+max-1
    y=rand()%15+1;
    }
    Last edited by aminpost; 02-25-2009 at 10:53 AM.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so your current code always gets the same set of random numbers because you are not calling srand() at he start of your program.

    The second problem is that you need to track which positions have been taken. There are many different ways, but the most obvious is to have say a 2D array that covers 1..15 (or 0..14) in both dimensions, and set it all to false/0, then when a position is taken, set it to true/1. So once a pair of coordinates have been picked, check if that position is already taken and pick a new one.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    17
    Quote Originally Posted by matsp View Post

    So once a pair of coordinates have been picked, check if that position is already taken and pick a new one.

    --
    Mats
    thx for your fast reply
    srand() added
    they position are (x,y)
    my problem is here,how?
    code of function?
    Last edited by aminpost; 02-25-2009 at 10:57 AM.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by aminpost View Post
    they position are (x,y)
    my problem is here,how?
    code of function?
    So how DO YOU think it should be done. I have described the overall mechanism, so it's just the implementation details that matter here. And I think you should be able to do that yourself.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Dec 2008
    Posts
    17
    Quote Originally Posted by matsp View Post
    So how DO YOU think it should be done. I have described the overall mechanism, so it's just the implementation details that matter here. And I think you should be able to do that yourself.

    --
    Mats
    i can check to find same position but after find and get new position it should be check again to find same position my problem is here

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    do {
        (x, y) = draw_a_pair_of_random_numbers();
    } while (these_numbers_already_exist(x, y));
    This might be the basic loop. Put that into a for loop to repeat 10 times.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Registered User
    Join Date
    Dec 2008
    Posts
    17
    who can find problem ?

    Code:
    #include <iostream.h>
    #include <time.h>
    #include <stdlib.h>
    
    class chess{
    
    public :
    	chess();  //constructor
    private :
    	int x;
    	int y;
    };
    
    main()
    {
    srand((unsigned)time(NULL));
    chess a[10];
    
    }
    
    chess::chess()  //get position in constructor
    {
    int k;
    do{
    
    x=rand()%15+1; //rand()%max+min will give values from min to min+max-1
    y=rand()%15+1;
    
    
    for(int j=1 ; j<=10 ; j++)
    {
    	k=0;
    	if(a[j].x==x && a[j].y==y)
    	break;
    	else
    	k=1;
    }
    
    } while(k);
    
    }

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You have the basic idea right. However that implementation is well dodgy - you are checking positions of internal components of an object that hasn't been constructed yet (except for the last object - assuming you fix the loop as suggested below, that is).

    You are also starting at index 1 and going to index 10 - which we discussed earlier, that if you have 10 elements, they have index from 0 to 9.

    Finally, I don't think it's a good idea for the constructor to know that there are 10 objects of it's own kind being constructed - it is breaking the basic principles of the way constructors work. You should either:
    1. Call the constrructor with validated x & y coordinates.
    2. Have a separate map of the coordinates taken [as I suggested originally].
    [Other valid alternatives also exist].



    [1] You MAY find that if you are filling ALL cells of a range that you should "pick the empty" instead of using random for the last few items - that way, you don't get an infinite loop if the random number generator isn't capable of giving you ALL the positions in your matrix - but I doubt that is a problem right now.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random number problems
    By lespaul5895 in forum C Programming
    Replies: 3
    Last Post: 07-05-2007, 02:16 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Replies: 2
    Last Post: 01-04-2004, 05:52 PM
  4. Random number generator
    By Caze in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2002, 08:10 AM
  5. Ask about generate Random number
    By ooosawaddee3 in forum C Programming
    Replies: 2
    Last Post: 07-01-2002, 04:30 AM