samedi 13 janvier 2024

What is the modulus of the libc pRNG LCG?

I'm a bit confused at the moment about the linear congruential generator used in the rand() function in stdlib to generate random numbers. The table on https://en.wikipedia.org/wiki/Linear_congruential_generator lists the modulus used by GCC and for ANSI C as 2^31. However, according to https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf p347, for ANSI C 2^32 is implicitly used. Does rand() in stdlib also use 2^32?

I've implemented and tested the ANSI C implementation, which works fine.




reduce a list of objects given an associated list of likelyhoods

I have a list (aka, slice) of objects. I also have a list of the same size that contains the likelyhood that each object will be useful in the future. I need to reduce my object list length so that it has n or fewer elements. (It may or may not have n+ elements to start.) How can I do this efficiently?

I was thinking I could sort the object list according to the probabilities and then truncate it. However, I want the low-probability objects to have a chance of remaining. I was also thinking that I could check the probability on each item against a random number to see if it lives or dies, but then I might end up with fewer than n items in the end.




vendredi 12 janvier 2024

Why are all the strings the same

For test my hashmap on C language I need to generate many keys (string). I wrote this code for generate keys. And this code doesn't works.

    char* keys [15] = { 0 };

    for (int i = 0; i < 15; i++)
    {
        char key [7] = { 0 };
        for(int j = 0; j < 5; ++j)
        {
            int rand_char = rand()%26+'A';
            key[j] = (char)rand_char;
        }
        *(keys + i) = key; 
    }

    for (int i = 0; i < 15; i++)
        printf("%s\n", keys[i]); 

The problem is all keys are the same and I don't know why this happens. OUPUT:

MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ
MPAPQ

I tried putting the random result into a variable (int rand_char) and also tried the debug keys during the generation process and saw that all the letters change when new letters are generated, as if all the lines were one string.

char* keys [15] = { 0 };

for (int i = 0; i < 15; i++)
{
    char key [7] = { 0 };
    for(int j = 0; j < 5; ++j)
    {
        int rand_char = rand()%26+'A';
        key[j] = (char)rand_char;
        for (int k = 0; k < i; k++)
            printf("%s\n", keys[k]); 
    }
    *(keys + i) = key; 
}

OUPUT:

B
BM
BMQ
B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
MPAPQ
MPAPQ



jeudi 11 janvier 2024

How do I get a random choice from a list to not give me duplicates?

Sorry if it's unclear, beginner here. Basically I have this to pick random questions for a quiz:

import random

econ = ["eq1", "eq2", "eq3", "eq4", "eq5", "eq6", "eq7", "eq8"]
mil = ["mq1", "mq2", "mq3", "mq4", "mq5", "mq6", "mq7", "mq8"]
oro = ["oq1", "oq2", "oq3", "oq4", "oq5", "oq6", "oq7", "oq8"]
pol = ["pq1", "pq2", "pq3", "pq4", "pq5", "pq6", "pq7", "pq8"]

cats = [econ,mil, oro, pol]
 
for x in cats:
        for y in range(3):
            print(random.choice(x))

However, I sometimes get duplicates, example: eq3 <- here eq3 <- here eq1 mq5 mq7 mq2 oq1 oq4 oq5 pq3 pq7 pq2

I've scoured through forums and the python coding book but was unable to find something that didn't confuse me more!

TL;DR: How do I avoid repetition of random items in the code?




mercredi 10 janvier 2024

Int won't print in the txt file [closed]

I am trying to print a random generated serial along with a name and a date in a txt file. The name and the date prints and get saved but only a question mark appears in the place of the supposed serial digits.

I tried changing the int to long but it does nothing, so does the suggestion.




vendredi 5 janvier 2024

Random.Next() generates same value forever. in Paralled.Foreachasync [duplicate]

I create a Random object at the start of my method. Then in that method I run a Parallel.Foreachasync loop.

Inside this I generate 2 random numbers using Random.Next(). I then check to see if the numbers are the different in a while loop. In the while loop I generate a new random number until they are different. For the first few thousand loops this works fine. Then suddenly it gets stuck generating the same number over and over again...

var room1Index = rnd.Next(0, slot1.RoomList.Count - 1);
var room2Index = rnd.Next(0, slot2.RoomList.Count - 1);

while (room1Index == room2Index)
{
    room2Index = rnd.Next(0, slot2.RoomList.Count - 1);
}

I have checked the roomlist count and it is 4 so I would expect the two numbers to be different at least after a few iterations... However it always generates 0

Why could this be happening?




jeudi 4 janvier 2024

Generating a random sequence of numbers in Python using a dash [closed]

It is necessary to make a random sequence of numbers in Python through a dash, for example, 46417635-7485-8418-8754-651578178453.

Using the random module and the randint function, it was possible to generate numbers in a certain interval (str(random.randint(100, 150). How to make a sequence of numbers separated by a hyphen?