Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/ILNumerics.2.14.4735.573/Functions/builtin/rand.cs @ 11194

Last change on this file since 11194 was 9102, checked in by gkronber, 12 years ago

#1967: ILNumerics source for experimentation

File size: 4.4 KB
Line 
1///
2///    This file is part of ILNumerics Community Edition.
3///
4///    ILNumerics Community Edition - high performance computing for applications.
5///    Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
6///
7///    ILNumerics Community Edition is free software: you can redistribute it and/or modify
8///    it under the terms of the GNU General Public License version 3 as published by
9///    the Free Software Foundation.
10///
11///    ILNumerics Community Edition is distributed in the hope that it will be useful,
12///    but WITHOUT ANY WARRANTY; without even the implied warranty of
13///    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14///    GNU General Public License for more details.
15///
16///    You should have received a copy of the GNU General Public License
17///    along with ILNumerics Community Edition. See the file License.txt in the root
18///    of your distribution package. If not, see <http://www.gnu.org/licenses/>.
19///
20///    In addition this software uses the following components and/or licenses:
21///
22///    =================================================================================
23///    The Open Toolkit Library License
24///   
25///    Copyright (c) 2006 - 2009 the Open Toolkit library.
26///   
27///    Permission is hereby granted, free of charge, to any person obtaining a copy
28///    of this software and associated documentation files (the "Software"), to deal
29///    in the Software without restriction, including without limitation the rights to
30///    use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
31///    the Software, and to permit persons to whom the Software is furnished to do
32///    so, subject to the following conditions:
33///
34///    The above copyright notice and this permission notice shall be included in all
35///    copies or substantial portions of the Software.
36///
37///    =================================================================================
38///   
39
40using System;
41using System.Collections.Generic;
42using System.Text;
43using System.Runtime.InteropServices;
44using ILNumerics.Storage;
45using ILNumerics.Misc;
46using ILNumerics.Native;
47using ILNumerics.Exceptions;
48
49namespace ILNumerics {
50
51  public partial class ILMath {
52
53        [ThreadStatic]
54        private static Random m_randomGenerator;
55        /// <summary>
56        /// Random number generator used for any generation of random numbers.
57        /// </summary>
58        /// <remarks>This property is thread safe (by using the [ThreadStatic] attribute).</remarks>
59        protected static Random RandomGenerator {
60            get {
61                if (m_randomGenerator == null) {
62                    m_randomGenerator = new Random(Environment.TickCount + System.Threading.Thread.CurrentThread.ManagedThreadId);
63                }
64                return m_randomGenerator;
65            }
66        }
67        /// <summary>
68        /// Pseudo random N-dimensional array elements.
69        /// </summary>
70        /// <param name="dimensions">int array or single int paramters specifying
71        /// dimensions for new array to be created.</param>
72        /// <returns>N-dimensional array filled with random numbers.</returns>
73        /// <remarks><para>The elements lie within the range 0.0 ... 1.0 and are uniformly
74        /// distributed.</para>
75        /// <para>The initial seed will be set to Environment.TickCount on the first call.</para></remarks>
76        public static ILRetArray<double> rand(params int[] dimensions) {
77            ILSize dim = new ILSize(dimensions);
78            double[] data = ILMemoryPool.Pool.New<double>(dim.NumberOfElements);
79            if (dim.NumberOfElements < ILNumerics.Settings.s_minParallelElement1Count) {
80                unsafe {
81                    fixed (double* pRetArray = data) {
82                        double* pCurIdx = pRetArray;
83                        double* pLastElement = pCurIdx + dim.NumberOfElements;
84                        while (pCurIdx < pLastElement)
85                            *pCurIdx++ = RandomGenerator.NextDouble();
86                    }
87                }
88            } else {
89                System.Threading.Tasks.Parallel.For(0, dim.NumberOfElements, i => {
90                    data[i] = RandomGenerator.NextDouble();
91                });
92            }
93            return new ILRetArray<double>(data, dim);
94        }
95
96    }
97}
Note: See TracBrowser for help on using the repository browser.