random

Uniform random positive decimal number in bash

北慕城南 提交于 2020-01-05 04:33:13
问题 I would like to make a random ICMP traffic using ping. I need to make a random decimal number between 0.5 to 1 for time parameter in my code. Could you please show me how to make uniform random decimal number? My code just can make a integer number. Thanks #!/bin/bash for ((i = 0; i <=100; i++)) do #need to have random number between 0.5 to 1 for time time=$(($RANDOM%5+1)) num=$(($RANDOM%5+1)) ping -c $num -i $time 10.0.0.2 done 回答1: bash doesn't actually do decimal number, only integers, but

How to return middle bits from random number generator?

北城以北 提交于 2020-01-05 03:47:09
问题 I have a random_rand() function which produces a random number between 0 and RANDOM_RAND_MAX. RANDOM_RAND_MAX is defined to be 65535. I would like to use the middle bits from the result of random_rand() instead of lowest-order bits so that I can make the randomiztion better. Could you please show me a quick way to do this. Thanks 回答1: That's naughty. Linear congruential generators work in such a way that the "most random" part comprises the lower order bits. A very famous IBM implementation

Shuffle a list and return a copy

安稳与你 提交于 2020-01-05 03:25:33
问题 I want to shuffle an array, but all I find was method like random.shuffle(x) , from Best way to randomize a list of strings in Python Can I do something like import random rectangle = [(0,0),(0,1),(1,1),(1,0)] # I want something like # disorderd_rectangle = rectangle.shuffle Now I can only get away with disorderd_rectangle = rectangle random.shuffle(disorderd_rectangle) print(disorderd_rectangle) print(rectangle) But it returns [(1, 1), (1, 0), (0, 1), (0, 0)] [(1, 1), (1, 0), (0, 1), (0, 0)]

Shuffle a list and return a copy

爱⌒轻易说出口 提交于 2020-01-05 03:25:33
问题 I want to shuffle an array, but all I find was method like random.shuffle(x) , from Best way to randomize a list of strings in Python Can I do something like import random rectangle = [(0,0),(0,1),(1,1),(1,0)] # I want something like # disorderd_rectangle = rectangle.shuffle Now I can only get away with disorderd_rectangle = rectangle random.shuffle(disorderd_rectangle) print(disorderd_rectangle) print(rectangle) But it returns [(1, 1), (1, 0), (0, 1), (0, 0)] [(1, 1), (1, 0), (0, 1), (0, 0)]

How can I generate a random number from a group of numbers (not a range)

╄→гoц情女王★ 提交于 2020-01-05 02:52:13
问题 I would like to be able to generate a random number from a group of numbers, like for example: 1, 2, 3, 12, 11, 23, 45, 54, 10, 10, 12, 23, 35, 24. Using the rand and the srand i can generate random numbers different each time but i would like to able to condition the outcomes by generating random numbers from a group. 回答1: Example code Take an Array int a[]={ 1, 2, 3, 12, 11, 23, 45, 54, 10, 10, 12, 23, 35, 24}; int i = rand()%14; // here 14 is number of element in array. i will receive a

Generate random BMP in CLI

六月ゝ 毕业季﹏ 提交于 2020-01-04 16:58:07
问题 I need a truly random BMP in order to test various lossy image compression algorithms. Ideally, this would not rely on any library and run in a Linux CLI. It should generate a random BMP given a certain width and height . 回答1: You can use ImageMagick (which is installed on most Linux distros by default) to generate an image of random noise like this: convert -size 300x200 xc:gray +noise random out.bmp where 300 is the width and 200 is the height (just examples). Other types of noise are

Random Seed in PIC18F

我是研究僧i 提交于 2020-01-04 15:17:28
问题 I'm going to run modified DES code(C language) on the PIC18F2550 microcontroller. For this I am using mplabx IDE v 2 and Mplab xc8 v 1.30. To modify the code, I need a random number so that each run will produce different numbers. I want to use the rand function but I need a good seed for Srand function! Good seed can be time, but since there is no such thing as a micro or I do not know!! 回答1: You can store an integer value in EEPROM. When the device boots, you use it as a seed and then

sample integer values with specified mean

五迷三道 提交于 2020-01-04 14:19:17
问题 I want to generate a sample of integer numbers in R with a specified mean. I used mu+sd*scale(rnorm(n)) to generate a sample of n values that has exactly the mean= mu but this generates floating-point values; I would like to generate integer values instead. For example, I would like to generate a sample of mean=4. My sample size n =5, an example of generated values would be {2,6,4,3,5}. Any ideas on how to do this in R while satisfying the constraint of a specific value of the mean? 回答1:

Random select is not always returning a single row

扶醉桌前 提交于 2020-01-04 13:11:27
问题 The intention of following (simplified) code fragment is to return one random row . Unfortunatly, when we run this fragment in the query analyzer, it returns between zero and three results. As our input table consists of exactly 5 rows with unique ID's and as we perform a select on this table where ID equals a random number, we are stumped that there would ever be more than one row returned. Note: among other things, we already tried casting the checksum result to an integer with no avail.

Generate a random number with no repeating digits?

匆匆过客 提交于 2020-01-04 10:11:10
问题 I'd like to generate a random number with each digit being in range from 0-9 and not repeating itself. Assume finite length of 4. 1234 qualifies, each composite digit is unique. 1123 does not, 1 is repeated How can this be done please? 回答1: To generate the digits: std::vector<int> vec = {0,1,2,3,4,5,6,7,8,9}; // or initialize from array if not c++11 std::random_shuffle(vec.begin(), vec.end()); vec.resize(4); And to join the digits into a single number: int number = 0; for (auto i = vec.begin(