|
1 | 1 | # Mass Name Generation |
| 2 | + |
2 | 3 | We first generate all the possible names, shuffle them, and then either use `next` (the simplest way) or maintain a `current_index` to get the name. |
3 | 4 | Note that selecting randomly from the list of all names would be incorrect, as there is a possibility of the name being repeated. |
4 | 5 |
|
@@ -31,14 +32,14 @@ numbers = (str(i).zfill(3) for i in range(1000)) |
31 | 32 | names = [l + n for l in letter_pairs for n in numbers] |
32 | 33 | ``` |
33 | 34 |
|
34 | | -After the name generation, the names are shuffled - using the [default `seed`][random-seed] in the `random` module (the current timestamp). |
| 35 | +After the name generation, the names are shuffled - using the [default `seed`][random-seed] in the `random` module (the current timestamp). |
35 | 36 | When the tests reseed `random`, this has no effect as the names were shuffled before that. |
36 | 37 |
|
37 | | -We then set `NAMES` to the iterable of names, and in `reset`, set the robot's name to the `next(name)`. |
| 38 | +We then set `NAMES` to the iterable of names, and in `reset`, set the robot's name to the `next(name)`. |
38 | 39 | If you are interested, you can read more on [`iter` and `next`][iter-and-next]. |
39 | 40 |
|
40 | 41 | Unlike the [on the fly approach][approach-name-on-the-fly], this has a relatively short "generation" time, because we are merely giving the `next` name instead of generating it. |
41 | | -However, this has a huge startup memory and time cost, as 676,000 strings have to be calculated and stored. |
| 42 | +However, this has a huge startup memory and time cost, as 676,000 strings have to be calculated and stored. |
42 | 43 | For an approximate calculation, 676,000 strings * 5 characters / string * 1 byte / character gives 3380000 bytes or 3.38 MB of RAM - and that's just the memory aspect of it. |
43 | 44 | Sounds small, but this might be a relatively significant startup cost. |
44 | 45 |
|
|
0 commit comments