random

How do I generate random but unique numbers in python?

梦想与她 提交于 2020-01-11 11:39:10
问题 I want to generate a random number, and that random number should be unique, meaning it should not be repeated or generated again. I have tried making a list where I append in every iteration and check if it is already present in the list or not. If it is present in the list, the number is not appended. But I don't think it is an effective method. So, please help me with it. import random #an empty list to append randomly generated numbers empty = [] while True: #checking from range 500 to

Creating a PRNG engine for <random> in C++11 that matches PRNG results in R

僤鯓⒐⒋嵵緔 提交于 2020-01-11 10:44:20
问题 This question is twofold. I am translating an R script into C++ that uses the L'Ecuyer combined multiple recursive generator (CMRG) as it's engine (in particular, MRG32k3a), which then returns a random number from the uniform distribution over the interval (0, 1). A minimal example in R is shown below: seednum<-100 # set seed set.seed(seednum, kind="L'Ecuyer-CMRG") # set RNG engine runif(1) # set distribution I want to be able to validate my results between the R script and the C++ code (as

Randomly select file in PHP

女生的网名这么多〃 提交于 2020-01-11 10:33:31
问题 I'd like to use the 'include' keyword in php to randomly select a file from a folder and output the contents. How would I do this? Thanks. 回答1: Assuming you know the folder where the files are and that the files are PHP files: $phpFiles = glob('/path/to/files/*.php'); if (empty($phpFiles) === false) { $randomFile = $phpFiles[array_rand($phpFiles)]; include($randomFile); } 回答2: glob() would read filenames into array. and then you can shuffle this array and pick a random item. 回答3: use glob to

Predictable Javascript array shuffle

≯℡__Kan透↙ 提交于 2020-01-11 09:43:28
问题 I'm trying to predictably shuffle javascript arrays the same way each time the webpage is loaded. I can shuffle the arrays randomly, but every time i reload the page it's a different sequence. I'd like it to shuffle the arrays the same way every time the page loads. There are many arrays and they are part of a procedurally generated world. 回答1: Chance.js worked perfectly. Thank you Billy Moon. My Example: <script type="text/javascript" src="assets/js/chance.js"></script> var chance1 = new

Pseudo Random Generator with same output

时光毁灭记忆、已成空白 提交于 2020-01-11 09:36:28
问题 I came across an article about Car remote entry system at http://auto.howstuffworks.com/remote-entry2.htm In the third bullet, author says, Both the transmitter and the receiver use the same pseudo-random number generator. When the transmitter sends a 40-bit code, it uses the pseudo-random number generator to pick a new code, which it stores in memory. On the other end, when the receiver receives a valid code, it uses the same pseudo-random number generator to pick a new one. In this way, the

How to choose a set of unique random numbers (no duplicates) using the jQuery.inArray method?

偶尔善良 提交于 2020-01-11 09:23:50
问题 This is a Javascript / jQuery question: I'm trying to generate six unique random numbers between 1 and 21 (no duplicates), using the jQuery.inArray method. Those six numbers will then be used to select six .jpg files from a group named logo1.jpg through logo21.jpg. Can anyone tell me what I'm doing wrong here? <div id="client-logos"></div> <script src="http://code.jquery.com/jquery-1.5.js"></script> <script type="text/javascript"> Show = 6; // Number of logos to show TotalLogos = 21; // Total

Reading random values from an array

百般思念 提交于 2020-01-11 08:24:11
问题 I have an array with a 14 strings. I want to display each of these 14 strings to the user without duplicates. The closest I got was creating an array of integers and shuffling their values, and then reading from the array of strings using one of the numbers from the int array as the index: //appDelegate.randomRiddles is an array of integers that has integer values randomly appDelegate.randomRiddlesCounter++; NSNumber *index=[appDelegate.randomRiddles objectAtIndex:appDelegate

Is Windows' rand_s thread-safe?

别来无恙 提交于 2020-01-11 08:12:08
问题 Just as in title. Is suspect it is, but I couldn't find it anywhere explicitly stated. And for this property I wouldn't like to rely on speculations. 回答1: If you use the multithreaded version of the CRT, all functions are thread safe, because any thread-specific information is stored in TLS. rand_s actually doesn't use state information in the first place, since it just calls an OS API, so question of thread-safety doesn't arise for rand_s. rand(), however depends on a seed value to generate

Re-ordering arbitrary an array of integers

穿精又带淫゛_ 提交于 2020-01-11 07:58:06
问题 I have a method that accepts as argument an array of integers and I'd just change arbitrary the order of its values public static int[] _game_number = new int[4]; public static int[] _current_number = new int[4]; public static void GetRandomTwentyFour() { HashSet<int> nums = new HashSet<int>(); for (int i = 0; i < 4; i++) { Random r = new Random(); nums.Add(r.Next(0, 4)); } List<int> liste = nums.ToList<int>(); _current_number = new int[] { _game_number[liste[0]], _game_number[liste[1]],

互动媒体技术—代码本色0-4章编程学习

大憨熊 提交于 2020-01-11 05:34:11
主题:创作一组编程习作,体现随机行为及牛顿运动学 要求:编程语言与工具:编程工具不限;作品:参考《代码本色》的第0~4章内容及其实例程序(自行在processing内下载),针对这5章分别编写1个习作(一共5个),每个习作都有不少于2个案例参考,且必须有一定的拓展;报告:写一篇文章(也可以多篇文章,但最好有一个总的导航文章),发表为博文/推文等形式,描述运用的规律,若用到了数学/物理/化学等学科中的知识,要用平实易懂的语言介绍原理,尝试运用凝练的数学语言表达(公式、方程、推导等),特别要描述出这些原理如何在作品中呈现的。 文章目录 主题:创作一组编程习作,体现随机行为及牛顿运动学 第0章,随机数 实现效果截图: 利用知识及原理: 核心代码: 第1章,向量 实现效果截图: 利用知识及原理 核心代码 第2章 力 实现效果截图: 利用知识及原理 核心代码: 第3章,震荡 实验效果截图 利用知识及原理 核心代码 第4章,粒子系统 实现效果截图 利用知识及原理 核心代码 第0章,随机数 实现效果截图: 利用知识及原理: 1.不停闪烁和改变大小的颜色的圆形是利用random函数产生随机数进行颜色和位置的变化 位置变化: void update() { float m=random(0,1); if(m<0.8){ position.x=random(0,640); position.y