Example #1
0
        public static T GetRandom <T>(this IEnumerable <T> items)
        {
            int count   = items.Count() - 1; // for 0 index start
            int element = Sampling.GetUniform(count);

            return(items.ElementAt(element));
        }
Example #2
0
        public T Sample()
        {
            if (!_normalized)
            {
                Normalize();
                _normalized = true;
            }

            var    sample = Sampling.GetUniform();
            double last   = 0;

            foreach (T t in _densities.Keys)
            {
                if (sample > last && sample <= _densities[t])
                {
                    return(t);
                }
                last = _densities[t];
            }

            throw new IndexOutOfRangeException("There was a problem");
        }