random

Create a 128 byte random number

拈花ヽ惹草 提交于 2021-01-27 07:36:15
问题 If the rand() function creates a random number that is 4 bytes in length, and I wanted to create a random number that is 1024 bits in length (128 bytes), is the easiest method to get this by concatenating the rand() function 256 times or is there an alternative method? #include <stdio.h> #include <string.h> int main(void) { const char data[128]; memset(&data, 0x36, 128); printf("%s\n", data); puts(""); printf("%d\n", sizeof(data)/sizeof(data[0])); puts(""); int i = 0; unsigned long rez = 0;

How to generate random matrix without repetition in rows and cols?

放肆的年华 提交于 2021-01-27 07:35:06
问题 How to generate random matrix without repetition in rows and cols with specific range example (3x3): range 1 to 3 2 1 3 3 2 1 1 3 2 example (4x4): range 1 to 4 4 1 3 2 1 3 2 4 3 2 4 1 2 4 1 3 回答1: This algorithm will do the trick, assuming you want to contain all elements between 1 and n %// Elements to be contained, but no zero allowed a = [1 2 3 4]; %// all possible permutations and its size n = numel(a); %// initialization output = zeros(1,n); ii = 1; while ii <= n; %// random permuation

C# weighted random numbers

霸气de小男生 提交于 2021-01-27 06:37:58
问题 I need help with the programming of a game. You open a chest and with a given probability you find an item. Item / Chance A / 10% B / 30% C / 60% Random random = new Random(); int x = random.Next(1, 101); if (x < 11) // Numbers 1..10 ( A -> 10% ) { do_something1(); d } else if (x < 41) // Numbers 11..40 ( B -> 30 % ) { do_something2(); } else if (x < 101) // Numbers 41..100 ( C -> 60 % ) { do_something3(); } Does this example really make sense, in terms of probability? Do you have another

C# weighted random numbers

给你一囗甜甜゛ 提交于 2021-01-27 06:36:14
问题 I need help with the programming of a game. You open a chest and with a given probability you find an item. Item / Chance A / 10% B / 30% C / 60% Random random = new Random(); int x = random.Next(1, 101); if (x < 11) // Numbers 1..10 ( A -> 10% ) { do_something1(); d } else if (x < 41) // Numbers 11..40 ( B -> 30 % ) { do_something2(); } else if (x < 101) // Numbers 41..100 ( C -> 60 % ) { do_something3(); } Does this example really make sense, in terms of probability? Do you have another

Find free port not in use for apps - find some algorithm

两盒软妹~` 提交于 2021-01-27 05:54:40
问题 I use the following API in my program to detrmine free port and provide it to application to run portscanner.findAPortNotInUse(3000, 65000, '127.0.0.1', function(error, port) { console.log('AVAILABLE PORT AT: ' + port) }) https://github.com/baalexander/node-portscanner This free port are given to application for use and working OK. The problem is that if I provide a free port to application A and the application is doesn't occupied it yet(sometimes it takes some time...) and there is coming

Find free port not in use for apps - find some algorithm

谁都会走 提交于 2021-01-27 05:54:40
问题 I use the following API in my program to detrmine free port and provide it to application to run portscanner.findAPortNotInUse(3000, 65000, '127.0.0.1', function(error, port) { console.log('AVAILABLE PORT AT: ' + port) }) https://github.com/baalexander/node-portscanner This free port are given to application for use and working OK. The problem is that if I provide a free port to application A and the application is doesn't occupied it yet(sometimes it takes some time...) and there is coming

Seeded Python RNG showing non-deterministic behavior with sets

可紊 提交于 2021-01-27 05:43:50
问题 I'm seeing non-deterministic behavior when trying to select a pseudo-random element from sets, even though the RNG is seeded (example code shown below). Why is this happening, and should I expect other Python data types to show similar behavior? Notes: I've only tested this on Python 2.7, but it's been reproducible on two different Windows computers. Similar Issue: The issue at Python random seed not working with Genetic Programming example code may be similar. Based on my testing, my

Efficient random number generation with C++11 <random>

六眼飞鱼酱① 提交于 2021-01-26 03:09:30
问题 I am trying to understand how the C++11 random number generation features are meant to be used. My concern is performance. Suppose that we need to generate a series of random integers between 0..k , but k changes at every step. What is the best way to proceed? Example: for (int i=0; i < n; ++i) { int k = i; // of course this is more complicated in practice std::uniform_int_distribution<> dist(0, k); int random_number = dist(engine); // do something with random number } The distributions that

Efficient random number generation with C++11 <random>

大兔子大兔子 提交于 2021-01-26 03:09:20
问题 I am trying to understand how the C++11 random number generation features are meant to be used. My concern is performance. Suppose that we need to generate a series of random integers between 0..k , but k changes at every step. What is the best way to proceed? Example: for (int i=0; i < n; ++i) { int k = i; // of course this is more complicated in practice std::uniform_int_distribution<> dist(0, k); int random_number = dist(engine); // do something with random number } The distributions that

Will time() ever return the same output?

限于喜欢 提交于 2021-01-20 13:21:52
问题 I am generating tokens for users in PHP when they register. I am wondering if two users could ever get the same token... as this will break the system. Please let me know if this is suffiecient. $token = md5(rand().time()); edit: i am now using a generate_uuid() function i found on another question. will this work? function generate_uuid() { return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0x0C2f ) | 0x4000,