mardi 1 mars 2016

How do I generate A-Z randomly to fill an array?

My assignment is to create a 3 part program which when you press 1, it runs method or "feature" 1, when you press 2, it runs feature 2, press 3, it runs feature 3. The end. I have the second feature figured out, but this feature (feature 1) is supposed to generate A-Z randomly to fill a 10x20 array. The code AS IS generates random integers and correctly fills and DISPLAYS a 10 x 20 grid. HOWEVER, how do I get it to display A-Z characters instead of numbers?

public class ModuleOnefor8 {

    public static void main(String[] args) {
        int[][] matrix = new int[10][20];

        for (int i=0; i<matrix.length; i++) {
            for (int j = 0; j<matrix[i].length; j++) {
                matrix[i][j] = (int)(Math.random()*'Z'-'A' + 1);

                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }
}




Python random character string repeated 7/2000 records

I am using the below to generate a random set of characters and numbers:

tag = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(36)])

I thought that this was a decent method. 36 character length, with each character being one of 36 unique options. Should be a good amount of randomness, right?

Then, I was running a query off an instance with what I thought was a unique tag. Turns out, there were SEVEN (7) records with the same "random" tag. So, I opened the DB, and ran a query to see the repeatability of my tags.

Turns out that not only does mine show up 7 times, but there are a number of tags that repeatedly appear over and over again. With approximately 2000 rows, it clearly should not be happening.

Two questions:

(1) What is wrong with my approach, and why would it be repeating the same tag so often?

(2) What would be a better approach to get unique tags for each record?

Here is the code I am using to save this to the DB. While it is written in Django, clearly this is not a django related question.

class Note(models.Model):
    ...
    def save(self, *args, **kwargs):
        import random
        import string
        self.tag = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(36)])
        super(Note, self).save(*args, **kwargs)




randomly pick an element from a list in python using pseudo random generator which should happen 50 percent of time

I have a list which has certain elements.For example event=["head","tail"].Each element should be picked randomly using pseudo random generator.This random event should occur roughly 50 percent of time.Use some sort of pseudo random number generator, but make this in a way that we can repeat the test or the sequence that way if something in the test fails, we can repeat what happened.This should be implemented in python




C++ - Extracting random numbers from /dev/urandom

I need many cryptographically secure numbers, so I was thinking about extracting randomness from /dev/urandom and then "converting" it into (say) unsigned long long int. I guess it should be very efficient and it seems it is cryptographically secure, but I will investigate this aspect more in the future.
Now the question is: how can I do so?

I found this code:

char * block;
short size = 1;
ifstream urandom("/dev/urandom", ios::in|ios::binary);
urandom.read(block,size);
urandom.close();

Does it make sense? And how do I convert what I get to the type I desire?

EDIT - Using random interface of C++11

Following a suggestion from the comments, I tried using a uniform distribution over the integers and a random_device initialized as /dev/urandom. Here is the code:

std::uniform_int_distribution<unsigned int> dist(0, modulus-1);
std::random_device urandom("/dev/urandom");
for(unsigned int i = start ; i < end ; ++i)
{
    vector[i] = dist(urandom);
}

The problem is that this code is approximately 1000 times slower than before (I was using a xorshift128+ generator): 5 milliseconds vs. almost 5 seconds. Is this normal? Honestly, I thought that streaming bytes in from /dev/urandom and converting them to unsigned int would have been way faster... Am I missing something?




How to use urandom to generate all possible base64 characters?

In a base64 digits you could save up to 6 bits (2**6 == 64).

Which means that you could fit 3 bytes in 4 base64 digits.

64**4 == 2**24

That's why:

0x000000 == 'AAAA'
0xFFFFFF == '////'

This means that a random string of 3 bytes is equivalent to a base64 string of 4 characters.

However if I am converting a number of bytes which is not a multiple of 3 in a base64 string, I will not be able to generate all the combination of the base64 string.

Let's take an example:

  • If I want a random 7 characters base64 string, I would need to generate 42 random bits (64**7 == 2**42).

If I am using urandom to get 5 random bytes I will get only 40 bits (5*8) and if ask for 6 I will get 48 bits (6*8).

Can I ask for 6 bytes and use a mask to short it down to 5 or will it break my random repartition?

One solution:

hex(0x123456789012 & 0xFFFFFFFFFF)
'0x3456789012'

Another one:

hex(0x123456789012 >> 8)
'0x1234567890'

What do you think?




Finding the area underneath y = x^4 in the domain 2 ≤ x ≤ 4 by using a Monte Carlo Method

In my approach, I will use a hypothetical rectangle with co-ordinates (2,0) , (4,0), (2, 256) and (4, 256). I will generate random xy co-ordinates within this rectangle and find the ratio between the number of co-ordinates that fall within the region defined by y ≤ x^4 and the number of co-ordinates that fall within the entire rectangle. Multiplying this by the area of the rectangle should give me the area under the graph.

I am struggling to generate random decimal xy co-ordinates in the defined rectangle. Any help would be much appreciated :)

I have only just started integration in school so my knowledge in this area is quite narrow as of now.

Here is my code:

public class IntegralOfX2 {

    public static double randDouble(double min, double max) {
        min = 2;
        max = 4;
        Random rand = new Random();
           double randomNum;
        randomNum = min + rand.nextDouble((max - min) + 1); // an error keeps occuring here

        return randomNum;
    }



    public static void main(String[] args) {

        double x = 0; // x co-ordinate of dart
        double y = 0; // y co-ordinate of dart
        int total_darts = 0; // the total number of darts
        int success_darts = 0; // the number of successful darts
        double xmax = 4;
        double xmin = 2;
        double ymax = 256;
        double ymin = 0;
        double area = 0;


        for (int i = 0; i < 400000000; i++) {
         //   x = randDouble(xmin, xmax);
         //   y = randDouble(ymin, ymax);


          x = xmin + (Math.random() * ((xmax - xmin) + 1));
          y = ymin + (Math.random() * ((ymax - ymin) + 1));

                    total_darts++;


            if (y <= (x * x * x * x)) {
                success_darts++;
            }

        }


       double ratio = (double)success_darts/(double)total_darts;
       area = ratio * 512;
       System.out.println(area);

    }
}




How do I make this play a random video? [on hold]

have a bit of code here that this site wont let me put so i uploaded it to zippy share as a txt file http://ift.tt/1oVuAvG