Random Array of 100 numbers ordered from lowest to highest

Pages: 12
@malibor, Your Generate function is misleading as it's interface suggests you can call it multiple times with different start and end values. But in fact it will only use the values passed on the first call.

You must also ensure that start <= end (maybe swap them if not).
Thanks, You're correct if the function is called multiple times it won't work as intended :/

But I was thinking about checking input arguments but omitted that part, anyway here is corrected function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <random>
#include <cassert>

typedef unsigned short USHORT;

inline USHORT Generate(USHORT start, USHORT end)
{
	assert(start < end);

	static std::random_device rand_dev;
	std::uniform_int_distribution<USHORT> dist(start, end);
	static std::mt19937 eng(rand_dev());

	return dist(eng);
}

Ran it, looks perfect, amazing
Topic archived. No new replies allowed.
Pages: 12