random

138. Copy List with Random Pointerv

删除回忆录丶 提交于 2020-01-23 05:49:38
138. 复制带随机指针的链表 给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。 要求返回这个链表的 深拷贝 。 示例: 输入: {"$id":"1","next":{"$id":"2","next":null,"random":{"$ref":"2"},"val":2},"random":{"$ref":"2"},"val":1} 解释: 节点 1 的值是 1,它的下一个指针和随机指针都指向节点 2 。 节点 2 的值是 2,它的下一个指针指向 null,随机指针指向它自己。 提示: 你必须返回 给定头的拷贝 作为对克隆列表的引用。 解法一: //时间复杂度O(n), 空间复杂度O(n) /* // Definition for a Node. class Node { public: int val; Node* next; Node* random; Node() {} Node(int _val, Node* _next, Node* _random) { val = _val; next = _next; random = _random; } }; */ class Solution { public: Node* copyRandomList(Node* head) { if(!head) return nullptr; if

How to use Python's random number generator with a local seed?

安稳与你 提交于 2020-01-23 05:14:32
问题 Python's random seems are global, so modules changing it will effect each other. While there are of course many 3rd party modules, is there a way using Python's standard library to have a random number local to a context. (without using random.get/setstate which may be problematic when mixing code from different modules). Something like... r = random.context(seed=42) number = r.randint(10, 20) Where each module can use its own random context. 回答1: From the docs: The functions supplied by this

How to use Python's random number generator with a local seed?

守給你的承諾、 提交于 2020-01-23 05:12:56
问题 Python's random seems are global, so modules changing it will effect each other. While there are of course many 3rd party modules, is there a way using Python's standard library to have a random number local to a context. (without using random.get/setstate which may be problematic when mixing code from different modules). Something like... r = random.context(seed=42) number = r.randint(10, 20) Where each module can use its own random context. 回答1: From the docs: The functions supplied by this

java实例之随机点名

一曲冷凌霜 提交于 2020-01-23 03:31:41
//导入包 import java.util.Random; class sjzh { //main方法 public static void main(String[] args) { //创建一维数组 String[] name={"x","y","z","o"}; //定义一个Random类型的变量r Random r=new Random(); //随机数作为下标赋给一个变量j int j = r.nextInt(name.length); //产生一个随机数,然后代表数组下标 //输出控制台数组name中j下标的元素 System.out.println(name[j]); } } 来源: https://www.cnblogs.com/sunmoonyou/p/9327069.html

Random Number Generation to Memory from a Distribution using VBA

孤者浪人 提交于 2020-01-23 01:25:26
问题 I want to generate random numbers from a selected distribution in VBA (Excel 2007). I'm currently using the Analysis Toolpak with the following code: Application.Run "ATPVBAEN.XLAM!Random", "", A, B, C, D, E, F Where A = how many variables that are to be randomly generated B = number of random numbers generated per variable C = number corresponding to a distribution 1= Uniform 2= Normal 3= Bernoulli 4= Binomial 5= Poisson 6= Patterned 7= Discrete D = random number seed E = parameter of

Python time & random模块

牧云@^-^@ 提交于 2020-01-23 00:35:10
time模块 三种时间表示 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp) : 通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。 格式化的时间字符串 元组(struct_time) : struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天,夏令时) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 import time # 1 time() :返回当前时间的时间戳 time.time() #1473525444.037215 #---------------------------------------------------------- # 2 localtime([secs]) # 将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。 time.localtime()

Generate random number in range excluding some numbers

笑着哭i 提交于 2020-01-22 20:27:48
问题 Is there a simple way in Python to generate a random number in a range excluding some subset of numbers in that range? For example, I know that you can generate a random number between 0 and 9 with: from random import randint randint(0,9) What if I have a list, e.g. exclude=[2,5,7] , that I don't want to be returned? 回答1: Try this: from random import choice print choice([i for i in range(0,9) if i not in [2,5,7]]) 回答2: Try with something like this: from random import randint def my_custom

Making functions that set the random seed independent

半腔热情 提交于 2020-01-22 13:36:26
问题 Sometimes I want to write a randomized function that always returns the same output for a particular input. I've always implemented this by setting the random seed at the top of the function and then proceeding. Consider two functions defined in this way: sample.12 <- function(size) { set.seed(144) sample(1:2, size, replace=TRUE) } rand.prod <- function(x) { set.seed(144) runif(length(x)) * x } sample.12 returns a vector of the specified size randomly sampled from the set {1, 2} and rand.prod

Making functions that set the random seed independent

最后都变了- 提交于 2020-01-22 13:35:27
问题 Sometimes I want to write a randomized function that always returns the same output for a particular input. I've always implemented this by setting the random seed at the top of the function and then proceeding. Consider two functions defined in this way: sample.12 <- function(size) { set.seed(144) sample(1:2, size, replace=TRUE) } rand.prod <- function(x) { set.seed(144) runif(length(x)) * x } sample.12 returns a vector of the specified size randomly sampled from the set {1, 2} and rand.prod

How can I create a unique random string in laravel 5 [duplicate]

落花浮王杯 提交于 2020-01-22 11:21:26
问题 This question already has answers here : PHP: How to generate a random, unique, alphanumeric string? (27 answers) Closed 4 years ago . I am new to laravel 5. I am working on a project where I want to assign some random-readable unique string to each application. I have knowledge of the each application id which may be use as a seed. Since the app is going to be use within the company I don't worry much about security. I expect the table size to grow so my goal is to achieve uniqueness as much