samedi 3 décembre 2022

How to find a random number in python

I have a random number and I want to create a bot that find it automatically but I'm stuck. Can you help me pls

I have these two variables:

a = random.randint(0,50)
b = 50

I want to make the bot find a using b. I tried this but it's too long to make:

if b != a:
    b = statistics.mean(0,b)
    if b > a:
        b = statistics.mean(0,b)
    elif b < a:
        min = b
        b = statistics.mean(min,50)
elif b == a:
    "GG"



Why do I get nearly the same number out of the rand() function in the first iteration?

in my C99-programme I want to use pseudo-random numbers between 0 an 1. Unfortunatly, my programme always generates a first number that is almost identical. It just steps up ever so slightly every time I rerun my programme.

This is the relevant part of my programme:

srand(time(NULL));  
for(int i = 0; i < 10; i++){
    float a = (float)rand()/RAND_MAX;
    printf("%f\n",a);

And here are the results of two Iterations with a time difference of below 10 seconds:

0.717103
0.357464
0.903628
0.271930
0.327478
0.917489
0.231215
0.026307
0.135259
0.290941

0.717221
0.330531
0.237708
0.151682
0.318986
0.201876
0.936884
0.209277
0.324705
0.311334

I tried to generate completely different numbers no matter how many I computed before, but the first is alway close to the same.




Generate random binary matrix constrained to no null row

I want to generate a random binary matrix, so I'm using W=np.random.binomial(1, p, (n,n)). It works fine, but I want a constraint that no row is just of 0s.

I create the following function:

def random_matrix(p,n):
m=0
while m==0:
    W = np.random.binomial(1, p, (n,n))
    m=min(W.sum(axis=1))
return W

It also works fine, but it seems to me too inefficient. Is there a faster way to create this constraint?




how to make a random matrix in bash in that program

#!/bin/bash

#How to make a random matrix in bash in that program, I don't understand how to make a random matrix in shell script.

#read the matrix order
read -p "Give rows and columns: " n

#accept elements
echo "Matrix element:"

let i=0
while [ $i -lt $n ]
do
let j=0
while [ $j -lt $n ]
do
read x[$(($n*$i+$j))]
j=$(($j+1))
done
i=$(($i+1))
done



vendredi 2 décembre 2022

How can I efficiently randomly select items from a dictionary that meet my requirements?

So at the moment, I have a large dictionary of items. Might be a little confusing, but each of these keys have different values, and the values themselves correspond to another dictionary.

I need to make sure that my random selection from the first dict covers all possible values in the second dict. I'll provide a rudimentary example:

Dict 1 = {key1: (A, C), key2: (B, O, P), key3: (R, T, A), and so on

Dict 2 = {A: (1, 4, 7), B: (9, 2, 3), C: (1, 3), etc

I need a random selection of Dict 1 to give me a coverage of all numbers from 1 - 10 in Dict 2 values.

At the moment, I am selecting 6 random keys from Dict 1, taking all the numbers that those letters would correspond with, and comparing that set to a set of the numbers from 1 - 10. If the selection isn't a subset of 1 - 10, select 6 more random ones and try again, until I have 1 - 10.

Now, this works, but I know it's far from efficient. How can I improve this method?

I am using Python.




Random.Choices IndexError: list index out of range

I am getting IndexError: list index out of range, what is wrong in my syntax ?

   #Working  
   filenames = random.sample(fnmatch.filter(os.listdir(IMAGE_PATH), '*.png'))
   #Not working
   filenames = random.choices(fnmatch.filter(os.listdir(IMAGE_PATH), '*.png'), k=10)
 
   for image_file in filenames:

     //Code here



Can't call a function from CountDownTimer object. What am I missing?

I'm going through Googles Kotlin Compose tutorials. One of the tasks is to build a game where you unscramble words. After completing it, I tried to improve the game on my own, and just can't figure out how to add a countdown timer to it. I want the program to skip a word when time runs out.

I'm a programming noob, it's not quite clear to me yet how classes and objects work and how they differ from functions.

The code for the timer at the moment:

object Timer: CountDownTimer(60000, 1000) {

    override fun onTick(millisUntilFinished: Long) {
        TODO("Not yet implemented")
    }

    override fun onFinish() {
        skipWord() // <<----------- **Unresolved reference: skipWord**
    }
}

Elsewhere in my code I have:

class GameViewModel : ViewModel() {

//....

fun skipWord() {    // <<---------------- Function that skips to the next word
    updateGameState(_uiState.value.score)
    updateUserGuess("")
}

//.....

private fun pickRandomWordAndShuffle(): String {
    // Continue picking up a new random word until you get one that hasn't been used before
    if (currentLanguage == "English") {
        currentWord = engWords.random()
    } else {
        currentWord = finWords.random()
    }

    setPointAmount()
    Timer.start() // <<---------------Start a new countdown for a new word.

    if (usedWords.contains(currentWord)) {
        return pickRandomWordAndShuffle()
    } else {
        usedWords.add(currentWord)
        return shuffleCurrentWord(currentWord)
    }
}

}

Also, a separate problem: the .random() always uses the same seed and picks the same words to unscramble.