|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using NumSharp.Generic; |
| 8 | + |
| 9 | +namespace NumSharp |
| 10 | +{ |
| 11 | + public partial class NumPyRandom |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// Draw samples from a uniform distribution. |
| 15 | + /// Samples are uniformly distributed over the half-open interval [low, high) (includes low, but excludes high). In other words, any value within the given interval is equally likely to be drawn by uniform. |
| 16 | + /// </summary> |
| 17 | + /// <param name="low">Lower boundary of the output interval. All values generated will be greater than or equal to low. The default value is 0.</param> |
| 18 | + /// <param name="high">Upper boundary of the output interval. All values generated will be less than high. The default value is 1.0.</param> |
| 19 | + /// <param name="size">Output shape. If the given shape is, e.g., m, n, k, then m * n * k samples are drawn. If size is None (default), a single value is returned if low and high are both scalars. </param> |
| 20 | + /// <returns>NDArray with values of type <see cref="double"/></returns> |
| 21 | + public NDArray uniform(double low, double high, params int[] size) |
| 22 | + { |
| 23 | + if (size == null || size.Length == 0) //return scalar |
| 24 | + { |
| 25 | + var ret = new NDArray<double>(new Shape(1)); |
| 26 | + var data = new double[] {low + randomizer.NextDouble() * (high - low)}; |
| 27 | + ret.SetData(data); |
| 28 | + return ret; |
| 29 | + } |
| 30 | + |
| 31 | + var result = new NDArray<double>(size); |
| 32 | + double[] resultArray = result.Data<double>(); |
| 33 | + |
| 34 | + //parallelism is prohibited to make sure the result come out presistant |
| 35 | + double diff = high - low; |
| 36 | + for (int i = 0; i < result.size; ++i) |
| 37 | + resultArray[i] = low + randomizer.NextDouble() * diff; |
| 38 | + |
| 39 | + result.SetData(resultArray); //incase of a view |
| 40 | + return result; |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Draw samples from a uniform distribution. |
| 45 | + /// Samples are uniformly distributed over the half-open interval [low, high) (includes low, but excludes high). In other words, any value within the given interval is equally likely to be drawn by uniform. |
| 46 | + /// </summary> |
| 47 | + /// <param name="low">Lower boundary of the output interval. All values generated will be greater than or equal to low. The default value is 0.</param> |
| 48 | + /// <param name="high">Upper boundary of the output interval. All values generated will be less than high. The default value is 1.0.</param> |
| 49 | + /// <param name="dType">The type of the output <see cref="NDArray"/></param> |
| 50 | + /// <returns></returns> |
| 51 | + public NDArray uniform(NDArray low, NDArray high, Type dType = null) |
| 52 | + { |
| 53 | + if (!low.shape.SequenceEqual(high.shape)) |
| 54 | + throw new IncorrectShapeException(); |
| 55 | + dType = dType ?? (low.dtype == high.dtype ? low.dtype : throw new IncorrectTypeException()); |
| 56 | + |
| 57 | + var ret = low + rand(low.shape).astype(dType) * (high - low); |
| 58 | + return dType != null ? ret.astype(dType) : ret; |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments