vendredi 4 novembre 2022

How can I compare three imageViews inside array to see and do something when all three matches?

Iam pretty new to Java and programming so need help. I want to make some fruit game when three same pictures lineup program will print "Jackpot".

For now RNG roll on click works, but need help how to check when all three pictures or (index) are the same in all ImageViews.

Thank you very much.

public class MainActivity extends AppCompatActivity {

    ImageView s1,s2,s3;
    TextView broj;
    Button roll;
    int images[]={R.drawable.banana,R.drawable.cherry,R.drawable.blackberry};
    int br=0;
    int i=0;
    int j=0;
    boolean flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        s1=findViewById(R.id.iv1);
        s2=findViewById(R.id.iv2);
        s3=findViewById(R.id.iv3);


        broj=findViewById(R.id.brojac);
        roll=findViewById(R.id.btnRoll);
        flag=true;

        roll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                getRandomImage();
                jackpot();
            }
        });
    }
    public void getRandomImage() {

        Random rng = new Random();
        s1.setImageResource(images[rng.nextInt(images.length)]);
        s2.setImageResource(images[rng.nextInt(images.length)]);
        s3.setImageResource(images[rng.nextInt(images.length)]);
        i++;
        if (i == 3) i = 0;

    }
    public void brojac()
    {

        br++;
        broj.setText(String.valueOf(br));


    }
    public void jackpot()
    {

    }
}

I don't know how to approach problem because lack of knowledge




jeudi 3 novembre 2022

How do I make a password generator that ends once all possible combinations under a character limit have been generated?

So I'm trying to create a password generator that continually generates completely random yet unique sequence of characters that have not been generated before. (the code stops once all possible combinations under the character limit variable have been generated.)

I have this but I understand that it generates duplicates and never ends.

import secrets
import string

letters = string.ascii_letters
digits = string.digits
special_chars = string.punctuation

alphabet = letters + digits + special_chars

while True:
    char_limit = secrets.choice(range(3, 5))
    pwd = ''
    for i in range(char_limit):
        pwd += ''.join(secrets.choice(alphabet))
    print(pwd)

Is there a library for this? A built-in feature? Am I going about it completely the wrong way? Thanks in advance.




How to shorten a repetitive part of basic python code? [duplicate]

I am new to coding, and just started learning Python last week. I made this Powerball number generator to practice:

import random

print(random.randrange(1, 70))

import random

print(random.randrange(1,70))

import random

print(random.randrange(1, 70))

import random

print(random.randrange(1,70))

import random

print(random.randrange(1, 70))

import random

print(random.randrange(1,27))

I am wondering, how can I shorten this, since the first 5 "import random prints" are exactly identical?

Thanks ahead for helping a noob!




Random Select 2 Records from all categories Groups in MariaDB

I'm trying to return the following result in a MariaDB online compiler (inspired from this old solution Select 2 products from each category in MySQL ):

Return random groups of 2 records from each categories of database test1.csv.

test1 dataset:

id,name,category
150,apple,fruit
153,orange,fruit
187,kiwi,fruit
189,banana,fruit
142,beef,meat
141,pork,meat
143,rabbit,meat
146,chicken,meat
410,pepsi,drinks
413,milk,drinks
418,coffee,drinks
415,mate,drinks
581,carrot,vegetables
586,tomato,vegetables
589,cucumber,vegetables
582,broccoli,vegetables

I reproduce the test on MariaDB Compiler here: https://onecompiler.com/mariadb/3ymv4a6e6

gif

mp4

Code:

-- create
CREATE TABLE test1 (
  id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  category TEXT NOT NULL
);

-- insert
INSERT INTO test1 VALUES (150,'apple','fruit');
INSERT INTO test1 VALUES (153,'orange','fruit');
INSERT INTO test1 VALUES (187,'kiwi','fruit');
INSERT INTO test1 VALUES (189,'banana','fruit');
INSERT INTO test1 VALUES (142,'beef','meat');
INSERT INTO test1 VALUES (141,'pork','meat');
INSERT INTO test1 VALUES (143,'rabbit','meat');
INSERT INTO test1 VALUES (146,'chicken','meat');
INSERT INTO test1 VALUES (410,'pepsi','drinks');
INSERT INTO test1 VALUES (413,'milk','drinks');
INSERT INTO test1 VALUES (418,'coffee','drinks');
INSERT INTO test1 VALUES (415,'mate','drinks');
INSERT INTO test1 VALUES (581,'carrot','vegetables');
INSERT INTO test1 VALUES (586,'tomato','vegetables');
INSERT INTO test1 VALUES (589,'cucumber','vegetables');
INSERT INTO test1 VALUES (582,'broccoli','vegetables');

-- fetch 
SET @prev=0,@rownum=0;
SELECT id, name, category 
FROM (
  SELECT *, 
         IF( @prev <> category, 
             @rownum := 1, 
             @rownum := @rownum+1 
         ) AS rank, 
         @prev := category, 
         @rownum  
  FROM (
    SELECT * FROM test1 
    ORDER BY category, rand()
  ) random_test1
) test1_ranked 
WHERE rank <= 2;

As shown above the code only returns 2 random records and from one single and same category.

An example of intended result on given dataset test1 would be:

2 records (or more, randomly selected) x 4 categories (all available categories, will be much more in production dataset)

For example:

150,apple,fruit
153,orange,fruit
142,beef,meat
141,pork,meat
418,coffee,drinks
415,mate,drinks
589,cucumber,vegetables
582,broccoli,vegetables

What is the way to control/expand the random selection to all categories and not just to a single one?

I looked at the order By and the rank and Rand docs but I couldn't find mentions for subcategories/groups application.

I found a similar python and pandas example of the exact expected result: 5. Pandas sample rows by group

df.groupby('color').apply(lambda x: x.sample(n=3)).reset_index(drop = True)

How to achieve the same result in MariaDb?

Your help is appreciated.




Edit file pairs and assign a random number [closed]

I need ideas for a personal non-profit project, I need to edit pairs of files (ex: a JPEG file and a TXT file) by assigning them the same random number.

The user can upload multiple pairs of files composed as follows:

1.jpeg - 1.txt

2.jpeg - 2.txt

The program must be able to randomize the pairs of files and assign them a random number.

Thanks so much for the answers

No code, Looking forward to inspirations




mercredi 2 novembre 2022

How do I generate numbers according to a parabolic distribution?

I have a function which plots the distribution of ions emitted as a function of the angle of emission. To simulate the emission I would like to sample values according to the function, but I'm struggling to do so.

The function is: enter image description here

where theta_0 is a constant.

I've tried to use the inversion method, but I can't seem to get it to work.




Want math.random() to generate different random number everytime (JavaScript) [closed]

I am making a dice game, for which I have to generate random number between 1-6.But I am only getting single number.

Console:

var randomNumber1 = Math.floor(Math.random() * 7) + 1;

undefined randomNumber1; 5 randomNumber1; 5 randomNumber1; 5