random

Creating array of length n with random numbers in JavaScript

浪子不回头ぞ 提交于 2020-01-09 19:07:13
问题 Following up on this answer for creating an array of specified length, I executed the below to get a corresponding result but filled with random numbers, instead of zeros. var randoms = Array(4).fill(Math.floor(Math.random() * 9)); Well, mathematically speaking it's random , all right. But I'd like the randomness to be visible within the array and not only between runs, of course. Stupid computer... Don't do what I say. Do what I want! I can iterate and put it my random (and varying) values.

Creating array of length n with random numbers in JavaScript

我只是一个虾纸丫 提交于 2020-01-09 19:05:05
问题 Following up on this answer for creating an array of specified length, I executed the below to get a corresponding result but filled with random numbers, instead of zeros. var randoms = Array(4).fill(Math.floor(Math.random() * 9)); Well, mathematically speaking it's random , all right. But I'd like the randomness to be visible within the array and not only between runs, of course. Stupid computer... Don't do what I say. Do what I want! I can iterate and put it my random (and varying) values.

带权重随机算法

放肆的年华 提交于 2020-01-09 19:04:59
闲言碎语不要讲,咱们直接上代码: 有什么讲的不对的地方,希望大家不吝赐教,感谢😊😊😊 let data = [ { weight : 20 , content : 'a' } , { weight : 40 , content : 'b' } , { weight : 30 , content : 'c' } , { weight : 10 , content : 'd' } ] ; /** 总权重法 */ function getRandom ( ) { let sum = 0 ; for ( let i = 0 ; i < data . length ; i ++ ) { sum += data [ i ] . weight ; } let random = Math . floor ( Math . random ( ) * sum ) ; console . log ( random ) ; sum = 0 ; for ( let i = 0 ; i < data . length ; i ++ ) { let weight = data [ i ] . weight + sum ; if ( random >= sum && random < weight ) { console . log ( data [ i ] ) ; return data [ i ] ; }

用python计算圆周率PI

a 夏天 提交于 2020-01-09 17:28:39
1.蒙特卡洛求圆周率 向区域内随即撒点 当点的数目足够多时,落在圆的点数目与在正方形点数目成正比 即圆的面积和正方形的面积成正比 可以得出计算圆周率的算法 DARTS=100000000 hits=0.0 clock() for i in range(1,DARTS+1): x,y=random(),random() dist=sqrt(x**2+y**2) if dist <=1.0: hits=hits+1 pi=4*(hits/DARTS) 2.安装tqdm来表示计算进度 在命令指令符中输入pip install tqdm来安装 3.计算圆周率与tqdm一起编码 from random import random from math import sqrt from time import * from tqdm import tqdm DARTS=100000000 hits=0.0 clock() for i in range(1,DARTS+1): x,y=random(),random() dist=sqrt(x**2+y**2) if dist <=1.0: hits=hits+1 pi=4*(hits/DARTS) for i in tqdm(range(10)): print ( "\r{:3}%\n" .format(i/10*100),end="") #

Why 1103515245 is used in rand?

对着背影说爱祢 提交于 2020-01-09 12:39:29
问题 I'm talking about this surprisingly simple implementation of rand() from the C standard: static unsigned long int next = 1; int rand(void) /* RAND_MAX assumed to be 32767. */ { next = next * 1103515245 + 12345; return (unsigned)(next/65536) % 32768; } From this Wikipedia article we know that the multiplier a (in above code a = 1103515245 ) should fulfill only 2 conditions: a - 1 is divisible by all prime factors of m . (In our case m = 2^32 , size of the int, so m has only one prime factor =

Select random row for each group

岁酱吖の 提交于 2020-01-09 10:25:07
问题 I have a table like this ID ATTRIBUTE 1 A 1 A 1 B 1 C 2 B 2 C 2 C 3 A 3 B 3 C I'd like to select just one random attribute for each ID. The result therefore could look like this (although this is just one of many options ATTRIBUTE B C C This is my attempt on this problem SELECT "ATTRIBUTE" FROM ( SELECT "ID", "ATTRIBUTE", row_number() OVER (PARTITION BY "ID" ORDER BY random()) rownum FROM table ) shuffled WHERE rownum = 1 however, I don't know if this is a good solution, as I need to

How to pick a random english word from a list [closed]

心不动则不痛 提交于 2020-01-09 09:52:06
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . What would be the best way to go about getting a function that returns a random English word (preferably a noun), without keeping a list of all possible words in a file before hand? 回答1: Word lists need not take

Generating a probability distribution

社会主义新天地 提交于 2020-01-09 09:24:05
问题 Given an array of size n I want to generate random probabilities for each index such that Sigma(a[0]..a[n-1])=1 One possible result might be: 0 1 2 3 4 0.15 0.2 0.18 0.22 0.25 Another perfectly legal result can be: 0 1 2 3 4 0.01 0.01 0.96 0.01 0.01 How can I generate these easily and quickly? Answers in any language are fine, Java preferred. 回答1: The task you are trying to accomplish is tantamount to drawing a random point from the N-dimensional unit simplex. http://en.wikipedia.org/wiki

Pick Random String From Array

孤街醉人 提交于 2020-01-09 04:29:10
问题 How do I go about picking a random string from my array but not picking the same one twice. string[] names = { "image1.png", "image2.png", "image3.png", "image4.png", "image5.png" }; Is this possible? I was thinking about using return strings[random.Next(strings.Length)]; But this has the possibility of returning the same string twice. Or am I wrong about this? Should I be using something else like a List to accomplish this. Any feedback is welcome. 回答1: The simplest way (but slow for large

Is there an easy way to randomize a list in VB.NET?

五迷三道 提交于 2020-01-09 03:50:06
问题 I have a list of type System.IO.FileInfo , and I would like to randomize the list. I thought I remember seeing something like list.randomize() a little while back but I cannot find where I may have seen that. My first foray into this yielded me with this function: Private Shared Sub GetRandom(ByVal oMax As Integer, ByRef currentVals As List(Of Integer)) Dim oRand As New Random(Now.Millisecond) Dim oTemp As Integer = -1 Do Until currentVals.Count = IMG_COUNT oTemp = oRand.Next(1, oMax) If Not