public static Graph2D CreateFullDigraph(int row, int col, RandomFunc rndFunc) { Graph2D graph = new Graph2D(row, col); int r = graph.row; int c = graph.col; for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { int cur = i * c + j; GraphNode node = graph[cur]; if (j < c - 1) { node.AddEdge(cur, cur + 1, rndFunc()); } if (j > 0) { node.AddEdge(cur, cur - 1, rndFunc()); } if (i < r - 1) { node.AddEdge(cur, cur + c, rndFunc()); } if (i > 0) { node.AddEdge(cur, cur - c, rndFunc()); } } } return(graph); }
/// <summary> /// Creates an array with random values /// </summary> /// <typeparam name="T">Type of the values</typeparam> /// <param name="size">the size of the array</param> /// <param name="channelEnableState">the channels that are enables(aka. get written with bytes)</param> /// <param name="rnd">the RandomFunc delegate providing the random numbers.</param> /// <param name="uniform">Should every channel receive the same value on the same pixel?</param> /// <returns>An array filled with random values of type T</returns> public static T[] CreateRandom <T>(int size, byte[] channelEnableState, RandomFunc <T> rnd, bool uniform) where T : struct { T[] buffer = new T[size]; WriteRandom(buffer, channelEnableState, rnd, uniform); return(buffer); }
/// <summary> /// Writes random values to a Memory Buffer /// </summary> /// <typeparam name="T">Type of the values</typeparam> /// <param name="instance">CLAPI Instance for the current thread</param> /// <param name="buf">MemoryBuffer containing the values to overwrite</param> /// <param name="rnd">the RandomFunc delegate providing the random numbers.</param> /// <param name="enabledChannels">the channels that are enables(aka. get written with bytes)</param> public static void WriteRandom <T>( CLAPI instance, MemoryBuffer buf, RandomFunc <T> rnd, byte[] enabledChannels) where T : struct { WriteRandom(instance, buf, rnd, enabledChannels, true); }
public static List <T> Shuffle <T>(IEnumerable <T> source, RandomFunc random) { var shuffled = new List <T>(); foreach (var item in source) { var selected = random(0, shuffled.Count); shuffled.Insert(selected, item); } return(shuffled); }
public static IEnumerable <T> Draw <T>(IList <T> source, RandomFunc random) { var indexes = CreateIndexes(source.Count); for (var i = indexes.Count; i > 0; --i) { var selected = random(0, i); var index = indexes[selected]; yield return(source[index]); indexes[selected] = indexes[i - 1]; } }
/// <summary> /// Writes random values to a MemoryBuffer /// </summary> /// <typeparam name="T">Type of the values</typeparam> /// <param name="instance">CLAPI Instance for the current thread</param> /// <param name="buf">MemoryBuffer containing the values to overwrite</param> /// <param name="rnd">the RandomFunc delegate providing the random numbers.</param> /// <param name="enabledChannels">the channels that are enables(aka. get written with bytes)</param> /// <param name="uniform">Should every channel receive the same value on the same pixel?</param> public static void WriteRandom <T>(CLAPI instance, MemoryBuffer buf, RandomFunc <T> rnd, byte[] enabledChannels, bool uniform) where T : struct { MemoryBuffer buffer = buf; T[] data = instance.commandQueue.EnqueueReadBuffer <T>(buffer, (int)buffer.Size); WriteRandom(data, enabledChannels, rnd, uniform); instance.commandQueue.EnqueueWriteBuffer(buffer, data); }
public static GraphBase TrianglesToGraph(IEnumerable <Triangle> triangles, RandomFunc rndFunc) { Dictionary <int, GraphNode> nodeMap = new Dictionary <int, GraphNode>(); List <GraphEdge> openEdges = new List <GraphEdge>(); int[] indices0 = new int[3]; int[] indices1 = new int[3]; foreach (Triangle triangle in triangles) { indices0[0] = triangle.v0; indices0[1] = triangle.v1; indices0[2] = triangle.v2; indices1[0] = triangle.v1; indices1[1] = triangle.v2; indices1[2] = triangle.v0; for (int i = 0; i < 3; i++) { int index0 = indices0[i]; int index1 = indices1[i]; nodeMap.TryGetValue(index0, out GraphNode node); if (node == null) { node = new GraphNode(index0); nodeMap[node.index] = node; } bool found = false; foreach (GraphEdge edge in openEdges) { if ((edge.from == index0 && edge.to == index1) || (edge.from == index1 && edge.to == index0)) { found = true; break; } } if (!found) { openEdges.Add(node.AddEdge(index0, index1, rndFunc())); } } } GraphBase graph = new GraphBase(); graph.nodes = nodeMap.Values.ToArray(); return(graph); }
/// <summary> /// Writes random values to a MemoryBuffer /// </summary> /// <typeparam name="T">Type of the values</typeparam> /// <param name="buf">MemoryBuffer containing the values to overwrite</param> /// <param name="rnd">the RandomFunc delegate providing the random numbers.</param> /// <param name="enabledChannels">the channels that are enables(aka. get written with bytes)</param> /// <param name="uniform">Should every channel receive the same value on the same pixel?</param> public static void WriteRandom <T>(MemoryBuffer buf, RandomFunc <T> rnd, byte[] enabledChannels, bool uniform) where T : struct { #if NO_CL T[] data = new T[1]; #else MemoryBuffer buffer = buf; T[] data = Instance._commandQueue.EnqueueReadBuffer <T>(buffer, (int)buffer.Size); #endif WriteRandom(data, enabledChannels, rnd, uniform); #if NO_CL Logger.Log("Writing Random Data to Buffer", DebugChannel.Warning); #else Instance._commandQueue.EnqueueWriteBuffer(buffer, data); #endif }
/// <summary> /// Writes random values to an array /// </summary> /// <typeparam name="T">Type of the values</typeparam> /// <param name="buffer">Array containing the values to overwrite</param> /// <param name="channelEnableState">the channels that are enables(aka. get written with bytes)</param> /// <param name="rnd">the RandomFunc delegate providing the random numbers.</param> /// <param name="uniform">Should every channel receive the same value on the same pixel?</param> public static void WriteRandom <T>(T[] buffer, byte[] channelEnableState, RandomFunc <T> rnd, bool uniform) where T : struct { T val = rnd.Invoke(); for (int i = 0; i < buffer.Length; i++) { int channel = i % channelEnableState.Length; if (channel == 0 || !uniform) { val = rnd.Invoke(); } if (channelEnableState[channel] == 1) { buffer[i] = val; } } }
private void Update() { if (Input.GetMouseButton(0)) { var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector2 dir = new Vector2(mousePos.x, mousePos.y); for (int i = 0; i < 100; i++) { Bullet bullet = ObjectPool.Instance.Dequeue(); if (bullet == null) { var gameObj = Instantiate(_bulletPrefab, this.transform.position, this.transform.rotation); bullet = gameObj.GetComponent <Bullet>(); } bullet.Init(this.transform.position, 10f); bullet.SetDirection(dir + RandomFunc.RandomVector2(-10f, 10f)); bullet.Move(); } } }
/// <summary> /// Writes random values to an array /// </summary> /// <typeparam name="T">Type of the values</typeparam> /// <param name="buffer">Array containing the values to overwrite</param> /// <param name="channelEnableState">the channels that are enables(aka. get written with bytes)</param> /// <param name="rnd">the RandomFunc delegate providing the random numbers.</param> public static void WriteRandom <T>(T[] buffer, byte[] channelEnableState, RandomFunc <T> rnd) where T : struct { WriteRandom(buffer, channelEnableState, rnd, true); }
/// <summary> /// Creates an array with random values /// </summary> /// <typeparam name="T">Type of the values</typeparam> /// <param name="size">the size of the array</param> /// <param name="channelEnableState">the channels that are enables(aka. get written with bytes)</param> /// <param name="rnd">the RandomFunc delegate providing the random numbers.</param> /// <returns>An array filled with random values of type T</returns> public static T[] CreateRandom <T>(int size, byte[] channelEnableState, RandomFunc <T> rnd) where T : struct { return(CreateRandom(size, channelEnableState, rnd, true)); }
public RandomTable(RandomFunc getRandom) { _getRandom = getRandom; }
/// <summary> /// runs params through RandomFunc() /// </summary> /// <param name="num">int from user</param> /// <param name="price">double from user</param> /// <param name="c">char from user</param> /// <param name="word">string from user</param> /// <returns>a string from the method called by RandomFunc()</returns> public string Getstring(int num, double price, char c, string word) { return(RandomFunc()(num, price, c, word)); }