jeudi 1 février 2018

Random button for method

I have two buttons (bt1, bt2). I need: if you click on button, it will redirect you to another activity, but when you press the second, it redirects you to the previous activity. But these two methods have to alternate (random) between the two buttons. What should I write?




Compare elements and return values larger than random number as true

I'm trying to compare each unique variable in one array px to a random number in another array py. If the element in px is greater than or equal to that of py than I want to note that value as True.

Here's some code.

import pandas as pd
import random

px = np.array([0.360617,0.360617,0.360617,0.989699,0.989699,0.989699,-1.020482])
py = np.random.uniform(low=0, high=1, size=len(px))

df = pd.DataFrame({'px': px, 'py': py, 'status': px >= py})

The resultant dataframe looks like this:

         px        py  status
0  0.360617  0.509826   False
1  0.360617  0.129870    True
2  0.360617  0.818778   False
3  0.989699  0.953721    True
4  0.989699  0.740662    True
5 -1.020482  0.302593    False

But I need it to look something like this:

         px        py  status
0  0.360617  0.509826   False
1  0.360617  0.509826   False
2  0.360617  0.509826   False
3  0.989699  0.953721    True
4  0.989699  0.953721    True
5 -1.020482  0.302593    False

whereby each unique px has an associated py. I'd like to see an example doing this with a for-loop and some other method such as groupby.




C# Can I identify what Random selects between the range?

I want to be able to determine what the value is selected when I get a random number in windows forms. For example, int mynum = numbers[rand.Next(0,7)]; I know the range is between 0 and 7, but I want to be able to identify what number is selected. e.g. numbers[3]

The following is the code I have. I have read binary values into an array int [] numbers and passed it through the following method. It generates my random 3 bit binary value, but I am making a small quiz where the number identified is the value the random number selects (e.g. the location 3 is the correct value for the item stored there, numbers[i] ==answer).

Is there a way to access this value?

public void generateBinary3bit(int[] numbers, Form1 f1)
        {
            Random rand = new Random();
            int mynum = numbers[rand.Next(0,7)];

            **int ans = numbers[rand];** 

            f1.lblBinaryText.Text = mynum.ToString();
        }

For context, I am reading in the values into the array here:

int [] numbers = new int[8];

  public void readNumbers(int[]numbers, Form1 F1)
        {
            try
            {
                StreamReader sr = new StreamReader(@"C:\BinaryNumbers.csv");

                while(sr.ReadLine() !=null)
                {
                    for (int i = 0; i < numbers.Length; i++)
                    {
                        numbers[i] = Convert.ToInt32(sr.ReadLine());
                        answer = i;
                    }
                }
                sr.Close();
            }
            catch ( Exception e)
            {
            }
        }

Thank you,




random sampling and probability

Sorry in advance since my question is more statistics than programming. But, I need the answer for further implementations. Consider that we have N records which X of them has specific features. We randomly select n records from N (sampling without replacement). What is the probability of appearing each of the X records in the sample n? OR how small would X get when it comes to the smaller group of n?

For example: N = 40,000,000 X = 20,000 n = 25,000,000




Java - get a random number in range with negative and positive number (-47; 56) without random.Math

How I get a random number in range with negative and positive number (-47; 56) per exemple -45, -5, 41 etc.. without random.Math. Thanks




Should `$srandom` apply to seed for `$urandom_range`?

Using ModelSim it looks like the seed random call with $srandom does not apply to subsequent random value generation with $urandom_range.

Running the process:

initial
  begin
  $srandom(42);
  $display("1: %0d, %0d, %0d", $urandom_range(1, 10), $urandom_range(1, 10), $urandom_range(1, 10));
  $srandom(42);
  $display("2: %0d, %0d, %0d", $urandom_range(1, 10), $urandom_range(1, 10), $urandom_range(1, 10));
  $srandom(42);
  $display("3: %0d, %0d, %0d", $urandom_range(1, 10), $urandom_range(1, 10), $urandom_range(1, 10));
  end

the output is:

# 1: 3, 1, 7
# 2: 9, 7, 2
# 3: 6, 4, 8

However, I had expected that line 1, 2 and 3 where identical, since $srandom was called be fore with same seed value.

Why are the generated values not the same?




Add a Rand to each row using Pandas' Assign and a Lambda Function

How do I pass a random number to a lambda function, so that I can be used by the pandas assign to add a different random number to each row.

My original attempt,

df = pd.DataFrame({'cat': [1]*10 + [0]*10,
                   'value': [3]*5 + [2]*5 + [2]*2 + [3]*8})

df.assign(cat=lambda df: df.cat + np.random.rand(1)).head(3)

Out[1]:
     cat        value
0   1.962857    3
1   1.962857    3
2   1.962857    3

We see here that the the random number 0.962857 has been added to all rows. But I would like a different rand for each row. How can I do this?