lundi 2 mai 2022

Unable to get an array of required size from random_seed subroutine

Based on what I understood from the description of RANDOM_SEED Subroutine here, https://gcc.gnu.org/onlinedocs/gfortran/RANDOM_005fSEED.html

I tried to get an array of size n by taking n as an input argument. However, I always get array of a fixed size (n = 32) no matter what I input in the argument of the executable. What am I doing wrong?

program test_random_seed
  implicit none
  integer, allocatable :: seed(:)
  integer :: n
 Character(len=2) :: inputnum

 call GETARG(1, inputnum)
 read (inputnum, *) n

  call random_seed(size = n)
  allocate(seed(n))
  call random_seed(get=seed)
  write (*, *) seed
  deallocate(seed)
end program test_random_seed

Outputs:

./test_random_seed.out 6
   270997589  -288181887  1948167863  -899467666   856399949  -616956193  -393084398  -169799794 -2106914961 -1327699024  -611308309  -956136276 -1047101846  1405583576 -2030082380  1642309723  -964872898  -478908624   486119102  1947066547  -722265400  -268681072   366932413  -791652926  1961977905  -349345081   465186042   808162632  -129440729  -683435376  1183730510   264749721           0
./tes_trandom_seed.out 4
   911554020  -223715509  -703604837 -1861804415  1952830407 -1126364511 -1115643071 -1251893035  1013863844  1767892501  1999133695 -1456139371 -1813327006  1135316397 -2034112324 -1863710425   749654783  1021195219  1479800515   882047462 -1297200646  1932542969 -1428989180  1078641551  -431962594 -1403398736   620407660   472937510  1677653929  1187629179  -357974360   953325445           0




python, randomly get keys from a dictionary based on values (with regex)

I have a dictionary like this,

ex = { 'a': 'tf', 'b': 'tf', 'c': '0fft', 'd': '0tfff', 'e': '0fft', 'f': 'tft', ... } where there are many same values for different keys. The values are combinations of 't', 'f', and '0'.

The tricky things are,

a), '0' is a wildcard and can be regarded as either a 't' or 'f'

b), two continuous 'f' can be regarded as either two 'f's or just one 'f'

I want to get a certain number of random keys from the dictionary based on their values:

def get_random(dictionary, count, feature):
    candidate = [k for k,v in dictionary.items() if v == feature]
    return random.sample(candidate, count)

An expected example: get_random(ex, 3, 'tft') will return ['c', 'e', 'f']. And 'd' ('0tfff') can be selected if the input feature is 'ftfff', 'ttfff', 'ftff', or 'ttff'.

I guess I can use regex to do this, but I have no idea how to implement it into the function.




Is there a way I can pick a random text file from a directory using C# unity? [closed]

I am trying to find out a way I can pick a random .txt file from a directory using C# unity2d and display it in a canvas. Is there a specific code to do it? TIA




dimanche 1 mai 2022

Is there a python package that generates a randwon word based on a cateogry like "feeling" or "location" etc.?

I found a random word generator package but the closest thing to a category is it's PartsOfSpeech parameter. If I wanted to generate random words based on the above categories, how would I go about doing it?




numpy multiple random choice

I want to generate multiple random choices without replacement from a range with numpy.

For example - I want that my output will be a 2D array, with x lines, where each line has exactly y randomly chosen numbers from the given range.

numpy.random.Generator.choice function can choose values, but you can't use the size parameter for each line separately (it represents the total number of choices)

I can call choice in a loop, but I'm looking for something more efficient.

My current code:

rand = numpy.random.default_rng()
output = numpy.zeros((x,y))

for i in range(x):
    output[i, :] = rand.choice(x, y, replace=False, shuffle=False)



How to generate and save random number in java? [closed]

I need randomly generated number to be saved, to use it as a pin code

I have separate method for generating random 5 digit pin

private static int getPin{
    Random random = new Random;
    int n = random.nextInt(100000);
    return n;
}

Integer n that was generated in method is than printed out

Client should write this number and be able to enter the project

But for some reason number n constantly changes, and so it is impossible for Client to know the pin code

How to save number n, so it would not change and could be used in different parts of my code




Why would you add a random variable to the shuffle method?

I don't understand the use of an additional random inside the shuffle variable. From the examples I've seen so far, there's not benefit to it, or is there? It says that it specifies the source of randomness, however, isn't the shuffle method in itself random? So why would I want to specify it any further?

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);

//What is the difference?
Collection.shuffle(list);
Collection.shuffle(list, new Random(2));