mercredi 4 mars 2020

Generate 20 random number with length 16 digit in C

I am using NXP(MiMxRt1020) microcontroller and I want to generate 20 random number which should be in length of 16 digit and store it into uint8_t random_number[20][16] in C.




Filling an Array with Random numbers, but the rand-Method doesnt work fast enough?

I try to fill array by random [-1,0,1] like this, but outputs like that: -1,-1,-1,-1,-1,-1,-1, 0, 0, 0, 0, 0... So it changes to slow? Do i have to set a new seed every time?

Random rnd = new Random();

            int z = rnd.Next(2);
            z -= 1;
            int q = rnd.Next(2);
            q -= 1;



Unit tests with Mocks on Mastermind and Random() Class

I need to programm Mastermind and ive done this with a random key. I now need to Unit test them. its easy for the control methods in my Helperclass but i dont know how to really mock or test the method with a Random Object from Class Random or a method using an Object from Scanner Class here the Code:

public int[] randomKey(){
        int[] key = new int[4];
        Random random1 = new Random();
        for(int i = 0;i < 4;i++) {
            key[i] = random1.nextInt(6);//Random Zahl for the Key
    }
        return key;
    }

Thats the Random method in HelperClass How you do there the Mock object and test

private String[] guess(){ //Input from guessing
        int k = 1;
        Scanner eingabewert = new Scanner(System.in);
        String[] guess = new String[4];
        for(int i = 0; i < 4;i++){
            k = 1;
            while(k==1) {
                System.out.println("Jetzt die " + (i + 1) + ".te Farbe eingeben ");
                guess[i] = eingabewert.nextLine();
                if(guess[i].equals("red")||guess[i].equals("blue")||guess[i].equals("yellow")||guess[i].equals("green")||guess[i].equals("purple")||guess[i].equals("brown")){
                    k = 0;
                }
            }
        }
        return guess;
    }

my helpless try was in the Test class for the method with the Random() Object

    @Mock
    private java.util.Random random;
    @InjectMocks
    private HelperClass helperClass;
    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
    }
    @Test
            void testRandomKey(){
            Random mockedItem = Mockito.mock(Random.class);
            when(mockedItem.nextInt(6)).thenReturn(1);


    }


}

I dont know how to go on or basically understood how this actually works pls help me and try to find a short solution for the test. okay if its just to the randomKey method. Im using Intellij Idea as IDE btw.




mardi 3 mars 2020

PHP: Get value on basis of probability

I have below array

$a = [
 'c1' => '10',
 'c2' => '20',
 'c3' => '10.50',
 'c4' => '19.99'
];

c1, c2, c3, c4 is Value we need on basis of probability 10, 20, 10.50, 19.99 .

So may be need to use rand() or mt_rand() function, so each time on basis of probability it gives random value from array.

Output can be c2 at 1st time, second time c4, third time may be c2 again

How to achieve this?




Benchmarking "sample" function in R

I was benchmarking the sample function in R and comparing it with igraph:sample_seq and ran into a strange result.

When I run something like:

library(microbenchmark)
library(igraph)
set.seed(1234)
N <- 55^4
M <- 500
(mbm <- microbenchmark(v1 = {sample(N,M)}, 
                       v2 = {igraph::sample_seq(1,N,M)}, times=50))

I get a result like this:

Unit: microseconds
 expr       min        lq        mean     median        uq       max neval
   v1 21551.475 22655.996 26966.22166 23748.2555 28340.974 47566.237    50
   v2    32.873    37.952    82.85238    81.7675    96.141   358.277    50

But when I run, for example,

set.seed(1234)
N <- 100^4
M <- 500
(mbm <- microbenchmark(v1 = {sample(N,M)}, 
                      v2 = {igraph::sample_seq(1,N,M)}, times=50))

I get a much faster result for sample:

Unit: microseconds
 expr    min     lq     mean  median     uq     max neval
   v1 52.165 55.636 64.70412 58.2395 78.636  88.120    50
   v2 39.174 43.504 62.09600 53.5715 73.253 176.419    50

It seems that when N is a power of 10 (or some other special number?), sample is much faster than other smaller N that are not powers of 10. Is this expected behavior or am I missing something?




How do I randomly select a generic type without repetitions

I have a coding assignment which I'm about 90% of the way through. The task is to randomly assign seats to a randomly selected person. I am having troubles with my drawItem() method in the Generic class, as I am supposed to eliminate it so the same number or person name doesn't repeat when being drawn.

The assignment description says: drawItem() – to randomly select an object from the box and return it, as well as eliminating it from the box (if the box is empty return null)...

Any help with the eliminating the object is greatly appreciated!! I have been stuck on this part of the assignment for too long now.

Here is my GenericDriver class:


    public static void main(String[] args) {

        Generic<String> myStringBox = new Generic<String>();

        // use the .add() function to add slips of papers with names on
        // them to fill the box
        myStringBox.add("Person 1");
        myStringBox.add("Person 2");
        myStringBox.add("Person 3");
        myStringBox.add("Person 4");
        myStringBox.add("Person 5");
        System.out.println("... names are being added!");

        Generic<Integer> myIntegerStack = new Generic<Integer>();
        // use the .add() function to add the integer slips of paper to the box
        myIntegerStack.add(1);
        myIntegerStack.add(2);
        myIntegerStack.add(3);
        myIntegerStack.add(4);
        myIntegerStack.add(5);
        System.out.println("... numbers are being added!");

        System.out.println("..... Generating which seat a person will occupy .....\n");

        System.out.println("First draw: " + myStringBox.drawItem()
            + " will occupy seat number: " + myIntegerStack.drawItem());

        System.out.println("Second draw: " + myStringBox.drawItem()
            + " will occupy seat number: " + myIntegerStack.drawItem());

        System.out.println("Third draw: " + myStringBox.drawItem()
            + " will occupy seat number: " + myIntegerStack.drawItem());

        System.out.println("Fourth draw: " + myStringBox.drawItem()
            + " will occupy seat number: " + myIntegerStack.drawItem());

        System.out.println("Fifth draw: " + myStringBox.drawItem()
            + " will occupy seat number: " + myIntegerStack.drawItem());

    }

} 

Here is my Generic class:

import java.util.Arrays;
import java.io.*;
import java.util.*;
import java.util.Collections;
@SuppressWarnings("unchecked")

public class Generic<T> {

    // creating an array
    T[] item;
    // count of the object's size
    int size;

    //generic constructor that will initilize the items in the box
    Generic (){
        item = (T[]) new Object[5];
    }

    public void add(T t) {
        item[size] = t;
        size++;
    }

    Random randomDraw = new Random();

    public T drawItem() {
        return item[randomDraw.nextInt(item.length)];
    }

    public String toString(){
        return Arrays.toString(item);
    } 

Here is a sample output to visualize what I mean:

..... Generating which seat a person will occupy .....

First draw: Person 4 will occupy seat number: 1
Second draw: Person 5 will occupy seat number: 1
Third draw: Person 2 will occupy seat number: 1
Fourth draw: Person 3 will occupy seat number: 3
Fifth draw: Person 1 will occupy seat number: 5



Python: Ensure that the sum of two randomly generated numbers is equal to the sum of another two randomly generated numbers

I am writing a program that calculates the turning forces (moments) on a uniform rod (where the centre of mass is in the middle). I need to split the length of the rod into four values, which themselves don't have to be equal; however, the sum of the first two values must equal the sum of the last two values. The total length of the rod must be randomly generated - so how do I do this?

Here is how I am randomly generating the numbers:

random_value_one = random.uniform(1, 5)
random_value_two = random.uniform(1, 5)
random_value_three = random.uniform(1, 5)
random_value_four = random.uniform(1, 5)
total_length_of_rod = random_value_one + random_value_two + random_value_three + random_value_four
centre_of_mass = total_length_of_rod / 2

Any advice would be greatly appreciated - thanks!