random values between a range

Apr 14, 2013 at 4:05am
Hello guys,

i have to find 2 random values between a range, lets say from 0-3 i have to find all the possible combinations between this range like (0,0),(0,1)...etc But, it has to be RANDOM and the same combination cannot repeat it self(obviously).

Thank you.
Apr 14, 2013 at 6:33am
I didn't get you. Please elaborate.
Apr 14, 2013 at 7:52am
ok i will try a different example, if i have 2 dices in a game which have numbers from 0-3 on each side and i roll the 2 dices, i would like to get all the possible combinations from these dices RANDOMLY.
Apr 14, 2013 at 8:07am
You do understand that if you trow dices two times the might get you the same value randomly, right? You should also tell if two dices are distinct or same (if combinations (1; 2) and (2; 1) are different). If you want all combination randomly shuffled, you might generate all combinations first and then shuffle it.

BTW. The best way to show what do you want is to give sample input and output with nessesary comments.
Apr 14, 2013 at 8:16am
thank you for the reply,

what im tryin to make is a small board game with randomly determined x,y locations for the dice , i already have the possible combinations(for 16 dice) they are:
0, 0 dice1
0, 1
0, 2
0, 3
1, 0
1, 1
1, 2
1, 3
2, 0
2, 1
2, 2
2, 3
3, 0
3, 1
3, 2
3, 3

what i would like to do is randomly shuffle these x,y locations so that the "Dice" can end up in a random position when i shuffle the board. I apologize for not being clear but hopefully this makes sense.

Thank you
Last edited on Apr 14, 2013 at 8:21am
Apr 14, 2013 at 9:03am
SO you want your dice array look like
 (0, 2) (3, 0) (1, 0) (2, 3) (0, 1) ... etc (completely random)
If so, you can first make something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include<algorithm>
#include<vector>
#include<utility>

int main()
{
    std::vector<std::pair<int, int>> values;
    /*initialize all combination*/
    for(int i = 0; i < 4; ++i)
        for(int j = 0; j < 4; ++j)
            values.emplace_back(std::make_pair(i,j));
    /*mix them randomly*/
    std::random_shuffle(values.begin(),values.end());
    /*output them to console*/
    for(const auto& x: values)
        std::cout << '(' << x.first << ", " << x.second << ')' << std::endl;
}

It gave me:
(3, 0)
(0, 1)
(2, 1)
(0, 2)
(0, 0)
(2, 3)
(1, 3)
(0, 3)
(1, 0)
(3, 3)
(2, 0)
(1, 1)
(3, 2)
(3, 1)
(2, 2)
(1, 2)
Last edited on Apr 14, 2013 at 9:10am
Apr 14, 2013 at 6:15pm
Thank you
Apr 14, 2013 at 6:35pm
i have one last question about this, if i have 16 buttons how can i distribute these (x,y) locations randomly to all those buttons?
Thank you
Apr 14, 2013 at 7:03pm
Something along the lines of
1
2
3
4
for(int i = 0; i < values.size(); ++i) {
    buttons[i].x = values[i].first;
    buttons[i].y = values[i].second;
}

Or alternatively you can create array of buttons before and then random_shuffle() it
Topic archived. No new replies allowed.