random

How to pick random element in an array AVOIDING (EXCEPT if it's) a certain value?

℡╲_俬逩灬. 提交于 2020-06-27 15:55:14
问题 For example, when users are connecting to this application they are given a userid var, then it's inserted into the array. So, i'm using chosenUser = usersOnlineArray[Math.round((Math.random()*usersOnlineArray.length))]; to pick another random user. But i need to make sure it doesn't pick the userID they personally were assigned so they don't end up getting themselves. how would i use that code but tell it to "remember, make sure you don't pick userid" maybe I could do something like

C# Random does not work like a random

别来无恙 提交于 2020-06-27 12:48:15
问题 I have a graph, each node have 4 child nodes. I wrote a algorithm to generate a random path from a begin node to an end node. At each node, it chooses a random next node. Visited node can be revisited. the code is like the following: public List<Node> GetPath(Node begin, Node end) { var nodes = new List<Node>(); var node = begin; while (node != end) { nodes.Add(node); var next = node.Children[new Random().Next(4)]; node = next; } nodes.Add(end); return nodes; } But sometimes, the Random does

Implementing specific distribution in python

余生长醉 提交于 2020-06-27 11:19:14
问题 i want to return 1<l<10 with probability 1/(2^(l-1)) how i should do this rather then: x = random() if x < 0.5: return 2 and so on thank you 回答1: This is going to be fun... I am a bit rusty with these things, so a good matematician could fix my reasoning. To generate a distribution from a formula you need first to do some integrals and calculate the cumulative density function for the specified interval. In particular we need to start to calculate the normalization constant. This integral

Implementing specific distribution in python

|▌冷眼眸甩不掉的悲伤 提交于 2020-06-27 11:17:58
问题 i want to return 1<l<10 with probability 1/(2^(l-1)) how i should do this rather then: x = random() if x < 0.5: return 2 and so on thank you 回答1: This is going to be fun... I am a bit rusty with these things, so a good matematician could fix my reasoning. To generate a distribution from a formula you need first to do some integrals and calculate the cumulative density function for the specified interval. In particular we need to start to calculate the normalization constant. This integral

C++ random non-repeated integers with weights

让人想犯罪 __ 提交于 2020-06-27 11:13:46
问题 I want to efficiently generate a random sample of unique (non-repeated) integers in a (closed) range [0, rnd_max] , with each number in the range being possible to choose, and each being associated with a sample weight (the more weight, the more likely it should be that the number is chosen, with probability exactly weight[i] / sum(weight[not_taken]) to be chosen next if it's not already taken in the sample). I see C++ has std::discrete_distribution which can generate random weighted integers

Generating/Placing k random items in a 2d array

前提是你 提交于 2020-06-27 06:17:21
问题 I have a 2d array, matrix of a sort (m x n). I need to generate '1' in k cells, but the probability of it should be equal for each cell . for example, if k=3, we pick randomly where to place the 3 '1's : [0, 0, 0, 0] [0, 1, 1, 0] [1, 0, 0, 0] At first, I tackled this by generating a Random of modulu m * n (rows * columns). But, that means that we could theoretically get to the end of the matrix without generating a single '1'. Then, I read about Yates Shuffle , but wasn't sure whether that's

Generating/Placing k random items in a 2d array

陌路散爱 提交于 2020-06-27 06:17:01
问题 I have a 2d array, matrix of a sort (m x n). I need to generate '1' in k cells, but the probability of it should be equal for each cell . for example, if k=3, we pick randomly where to place the 3 '1's : [0, 0, 0, 0] [0, 1, 1, 0] [1, 0, 0, 0] At first, I tackled this by generating a Random of modulu m * n (rows * columns). But, that means that we could theoretically get to the end of the matrix without generating a single '1'. Then, I read about Yates Shuffle , but wasn't sure whether that's

The fastest random number Generator

柔情痞子 提交于 2020-06-17 01:30:30
问题 I'm intending to implement a random number generator via Swift 3. I have three different methods for generating an integer (between 0 and 50000 ) ten thousand times non-stop. Do these generators use the same math principles of generating a value or not? What generator is less CPU and RAM intensive at runtime (having 10000 iterations)? method A : var generator: Int = random() % 50000 method B : let generator = Int(arc4random_uniform(50000)) method C : import GameKit let number: [Int] = [0, 1,

JSON get unique random values from array to text input html and show a text when all values are shown

前提是你 提交于 2020-06-13 09:30:06
问题 Here is my option code and text box <select id="sel" class="form-control input-lg" data-live-search="true"> <option value="">-- Select Your Country--</option> </select><br/><br/><br/> <input type = "text" id = "txtName" class="form-control input-lg" /> </div> This is my JSON code [ { "country":"First", "coupon":["1","10","11"] }, { "country":"Second", "coupon":"2" }, { "country":"third", "coupon":"3" }, { "country":"fourth", "coupon":"4" }, { "country":"fifth", "coupon":"5" } ] i have

Use R to Randomly Assign of Participants to Treatments on a Daily Basis

爷,独闯天下 提交于 2020-06-10 02:34:06
问题 The Problem: I am attempting to use R to generate a random study design where half of the participants are randomly assigned to "Treatement 1" and the other half are assigned to "Treatment 2". However, because half of the subjects are male and half are female and I also want to ensure that an equal number of males and females are exposed to each treatment, half of the males and females should be assigned to "Treatment 1" and the remaining half should be assigned to "Treatment 2". There are