-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday51_random.cpp
More file actions
58 lines (47 loc) · 1.44 KB
/
day51_random.cpp
File metadata and controls
58 lines (47 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
Good morning! Here's your coding interview problem for today.
This problem was asked by Facebook.
Given a function that generates perfectly random numbers between 1 and k (inclusive), where k is an input, write a function that shuffles a deck of cards represented as an array using only swaps.
It should run in O(N) time.
Hint: Make sure each one of the 52! permutations of the deck is equally likely.
*/
/**
* Idea: I can start by picking one card among the 52 availables, then among the 51.. until only one
* last card stand.
*
* See Fisher-Yates (Knuth) shuffle, we don't need to add index each time we can just perform n rand swap
* But this seems to work too
*/
#include <gtest/gtest.h>
#include <stdlib.h>
using namespace std;
void shuffle(vector<int> &cards)
{
size_t len = cards.size();
int index = 0;
for (int i = len; i > 0; --i)
{
int card_index = (rand() % i) + index;
swap(cards.at(index), cards.at(card_index));
index++;
}
}
TEST(SUFFLE, shuffle)
{
vector<int> cards{0, 1, 2, 3};
vector<uint64_t> occurences(cards.size(), 0);
srand(1);
const size_t ite = 100000;
for (int i = 0; i < ite; i++)
{
shuffle(cards);
occurences.at(cards.at(0))++;
}
for (int i = 0; i < 4; i++)
cout << i << " " << 1.0 * occurences[i] / ite << endl;
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}