lundi 1 avril 2019

how to remove repeating numbers from the table of random numbers?

I wrote a program that generates twenty random numbers. the chance of repeating the program is about 1/3. How can I rebuild my program? could anyone help me?

        for (int i = 0; i <= 19; i++) {
            rand[i] = (int) (Math.random() * 60 + 1);
        }
        for(int i=0;i<=19;i++)
        {
            rand_back[i]=rand[i];
        }

        for (int i = 0; i<=19;) {
            for(int j=0;j<=19;j++) {
                //porównaj czy wsytapiła juz taka sama liczba
                if((rand[i]==rand_back[j])&&(j!=i)) {
                    rand[j]=(int) (Math.random()*60+1);
                }
                if(j==19){
                    j=0;
                    i++;
                }
                if(i==19) {
                    break;
                }
            }
            if(i==19) {
                break;
            }
        }




Pygame Platformer- Randomly Spawning Enemies

I am currently having an issue when trying to randomly spawn enemies into my platformer game. I have an enemy class class Enemy(pygame.sprite.Sprite): that has an init and move(self) function. Currently i have each instance of the enemy defined individually: enemy1 = Enemy(210,515,"Enemy.png") enemy2 = Enemy(705,515,"Enemy.png") enemy3 = Enemy(1505,515,"Enemy.png") During the main game loop i append each instance to a group: enemy_list = pygame.sprite.Group() enemy_list.add(enemy1) enemy_list.add(enemy2) enemy_list.add(enemy3) However i would rather that the enemeies spawned at random times in a random position so i hought i could do a check like this: if random.randrange(0,100) < 1: spawnEnemy = Enemy(400, 515, "Enemy.png") My issue is that i do not know how to now append the random eney to the enemy_list. Any ideas?




How to generate pseudo-random noise to modify DCT coefficients for steganography in JPEG images?

I'm doing an exercise on steganography in JPEG images on an 8x8 pixels block of a JPEG image.

I applied quantization matrix to the DCT coefficients of the 8x8 block and this are the values I calculated in a zig-zag sequence

ZigZagSequence = {36, -2, 0, -2, -1, -3, 1, -2, 0, -1, 0, 0, 1, 0, 1, 0,0,........,0};

The next step of this exercise is: "A pseudo random noise must be applied to each coefficient. A pseudo random generator of integer numbers with uniform distribution in [-k,+k] (the parameters a, c, X0 and m must be selected in an appropriate way) must be applied to compute the watermarked coefficients [c1, .., c64]

How can i generate this numbers?

I read that the JPEG images has Gaussian noise distribution and I think that all the 0 after the first fifteen numbers in the array don't have to be affected by the noise because that would affect RLE and Huffman compression, am I right?

How can I determinate those numbers?

The suggested algorithm is Lehmer's linear congruence method

multiplier   a 0<a<m
increase     c 0<=c<m
seed        Xn 0<=Xn<m

Xn+1 = (a*Xn + c)mod m```




Logic Functions for Random Boolean Network

I am developing a very basic random boolean network.

A RBN consists of N nodes, which can take values of zero or one (Boolean). The state (zero or one) of each node is determined by K connections coming from other (or the same) nodes. The connections are wired randomly, but remain fixed during the dynamics of the network, i.e. “quenched”. The way in which nodes affect each other is not only determined by their connections, but by logic functions, which are generated randomly, simply using lookup tables for each node, which take the states of the connecting nodes as inputs, and the state of the node as output. These also remain fixed (quenched) during the dynamics of the network.

We had an idea to code the logic functions for each connection. Developed a binary tree for each node, read all the connections and randomly the root select which one to use (NOR, XOR, AND ... etc).

I am having a problem coding this piece because I cannot see how to add these nodes. I have thought of counting all de 1's and add in the tree.

Matrix(int vertex_num){
            srand(5);
            this->vertex_num = vertex_num;
            adjMatrix = new int*[vertex_num];
            /*QVector<QVector<int>> testMatrix;*/
            for (int i = 0; i < vertex_num; i++){
                adjMatrix[i] = new int[vertex_num];
                for (int j = 0; j < vertex_num; j++){
                    adjMatrix[i][j] = rand()%2;
                }
            }
       }

I would like to receive these connections, define a random function, and return a 0 or 1 after testing all the connections.




How to get random number generator to work properly [duplicate]

This question already has an answer here:

My code deals with two dice (a 10 sided "fair" dice and a 20 sided "fair" dice) and using classes, arrays and a random number generator to generate random rolls of the two dice and their summation, but all my code spits out is "You rolled: 18". That is not very random.


#include <iostream>
#include <stdlib.h>

using namespace std;

class Dice
{
  private:
  int rollDice[2] = {};

  public:
  void setval1(int x)
  {
    rollDice[0] = x;
  }

  void setval2(int y)
  {
    rollDice[1] = y;
  }

  double getVal1()
    {
      return rollDice[0];
    }

  double getVal2()
  {
    return rollDice[1];
  }
};

int main()
 {
  Dice a;
  a.setval1(rand()%9+1);
  a.setval2(rand()%19+1);
  cout << "You rolled: " << a.getVal1() + a.getVal2();
}





Generate Random Number in 2D Vector C++

I am implementing a simple 2D vector class in C++ which initialize a 2D vector with a given size (number of row and column) and whether to randomize the value or not. I also implement the method to print the matrix to the console to see the result.

I have tried to run the code using GCC 8.3.0 in Windows (MSYS2) with flag "-std=c++17". Here is the code.

#include <random>
#include <iostream>
#include <vector>


class Vec2D
{
public:
    Vec2D(int numRows, int numCols, bool isRandom)
    {
        this->numRows = numRows;
        this->numCols = numCols;

        for(int i = 0; i < numRows; i++) 
        {
            std::vector<double> colValues;

            for(int j = 0; j < numCols; j++) 
            {
                double r = isRandom == true ? this->getRand() : 0.00;
                colValues.push_back(r);
            }

            this->values.push_back(colValues);
        }
    }

    double getRand()
    {
        std::random_device rd;
        std::mt19937 gen(rd());
        std::uniform_real_distribution<> dis(0,1);

        return dis(gen);
    }

    void printVec2D()
    {
        for(int i = 0; i < this->numRows; i++) 
        {
            for(int j = 0; j < this->numCols; j++)
            {
                std::cout << this->values.at(i).at(j) << "\t";
            }
        std::cout << std::endl;
        }
    }
private:
    int numRows;
    int numCols;

    std::vector< std::vector<double> > values;
};

int main()
{
    Vec2D *v = new Vec2D(3,4,true);

    v->printVec2D();
}

What I expected is a 2D vector with randomized value when 'isRandom' argument is true. Instead, I got vector with values being all the same. For example. when I run the code in my computer I got this:

0.726249        0.726249        0.726249        0.726249
0.726249        0.726249        0.726249        0.726249
0.726249        0.726249        0.726249        0.726249

My question is what is wrong with my C++ code? Thank you in advance for the answer.




How to match a random number with a number from a list

I am using a list to keep track of numbers and want to match up the lists index with a random number so I can subtract 1 from the value of that index.

import random
race_length = int(input("Choose the Length You Would Like You Race To Be 
(Between 5 and 15)"))
dice = ["1", "2", "3", "4", "5", "6" ]
cars=[
    ["1", race_length],
    ["2", race_length],
    ["3", race_length],
    ["4", race_length],
    ["5", race_length],
    ["6", race_length],
]
while race_length >0:    
    print("Press Enter to Roll the Dice")
    input()
    chosen = int(random.choice(dice))
    print(int(chosen))

What would I do so i can match the chosen match with the numbers in my list