mercredi 1 mai 2019

Simulation of random variables using Polar method

I have the following algorithm

Step 1. Generate u1 and u2~U(0,1)

Step 2. Define v1=2u1-1, v2=2u2-1 and s=v1^2+v2^2

Step 3. If s>1, come back to Step 1.

Step 4. If s<=1, x=v1(-2logs/s)^(1/2) and y=v2(-2logs/s)^(1/2)

Here is my approach to implement this algorithm in R:

    PolarMethod1<-function(N)
{

  x<-numeric(N)
  y<-numeric(N)
  z<-numeric(N)

  i<-1

  while(i<=N)
  {u1<-runif(1)
  u2<-runif(1)
  v1<-(2*u1)-1
  v2<-(2*u2)-1
  s<-(v1^2)+(v2^2)

  if(s<=1)
  {
    x[i]<-((-2*log(s)/s)^(1/2))*v1
    y[i]<-((-2*log(s)/s)^(1/2))*v2
    z[i]<-(x[i]+y[i])/sqrt(2) #standarization
    i<-i+1
  }
  else
    i<-i-1
  }

  return(z)
}
z<-PolarMethod1(100000)
hist(z,freq=F,nclass=10,ylab="Density",col="purple",xlab=" z values")
curve(dnorm(x),from=-3,to=3,add=TRUE)

The code, fortunately, does not mark any error and works quite well with N=1000 but when I change to N=10000, instead of making a better approach to the curve displays:

N=10000

contrast with N=1000 displays:

N=1000

Why is that?

Is there something wrong with my code? It's supposed to be better adjusted when N increases.

Note:I added the z in the code to include both variables in the output.




How to input a list of quantities into one function and print the output to console

I have a list of quantities (100,500,1000]. I want to generate random numbers between 0 and 10, 100 times, 500 times, 1000 times. I want to do this in one call and print the output to the console.

def random_array(n): #define function to generate the random array
    array_a = []
    for i in range(0, n, 1):
        array_a.append(randint(0, 10))
    return array_a




Creating a timed random word generator

I'm using Unity and coding in C#

Is it possible to write a script that chooses random words from an array in 20 second intervals?

I imagine it goes along the lines of using a Random.Range(0, 5); function and attach it to a Text UI game Object.

Sorry I'm really new at this and would greatly appreciate any help.




Different runs give way too different results using random numbers

I have a code that starts by producing a random vector x0, then does something to it and arrives at a number, y. How exactly the code goes from x0 to y is probably not important. The problem is this:

I generated 100 random x0 and ploted the resulting 100 values of y. They were all concentrated around 3. Then I simply run the code again, without changing anything. It generated another 100 random x0 and ploted the resulting 100 values of y. This time they were all concentrated around 0.

(I tried including a picture with the results, but got "An error occured when uploading the image")

This is very suspicious. Why should two different runs of the same code give such different results? In the first run ALL values of y were between 2 and 4, while in the second run ALL values of y were between -1 and 1.

It must be related to the random number generation. This is something I am not very familiar with.

The code starts like this:

clear
clc

n=2;
c=0.01/(n-1);
conec=c+0.03*rand(n,n);

for samp=1:100
x0=rand(1,2*n);
etc.

(it involves integrating a differential equation, and then some linear algebra, but the details should not matter, I think)




How to generate random string and put it in HashTable?

I want to generate a random unique String and put in hashtable. I did like below code but it is not unique and it is just One Character .

    while(k<4){
    for(int j=1 ; j<=13 ;j++){

    Hashtable<Integer, String> Deck = new Hashtable();  
         int myChar = (int) (Math.random()*str.length());
           StringBuilder sb = new StringBuilder();
            sb.append(str.charAt(myChar)); 

     int i = (int) (Math.random() *52) ;    
    Deck.put(i, sb.toString());
    System.out.print(Deck);
    }

    System.out.println();
    k++;
    }

output is this

{30=d}{9=e}{11=b}{10=d}{43=g}....

I want to be

{30=dea}{9=egt}{11=brf}{10=dgrtg}{43=grrh}{14=gwrt}.....




I can't solve this Ruby problem I found in an online course

I found a problem that I can't solve, in an online course. I need to script it in Ruby, but every time I arrive to the last part, I can't script it right.

This is the problem: (Ruby)

Write a "Deaf Grandma" program. Whatever you say to grandma (whatever you write to the program), she should respond with an 'EH ?! UP THE VOICE, CHILD!' Unless you shouted it (written in capital letters). If you shout then she can hear you (or so she thinks) and then she answers you 'NO, NOT FROM 1938 !' To make this program really believable, make sure that your grandmother shouts a different year all the time; maybe a random number between 1930 and 1950. You can't stop talking to your grandmother until you cry 'BYE'. Tip: Don't forget about chomp! 'BYE' with an Enter is not the same as a 'BYE' without! Tip 2: Try first to think about which parts of your program will have to repeat themselves again and again. All these parts will have to be in your while loop.

Thanks For Helping!




Randomness of Python's random

I'm using Python to generate images using dashed lines for stippling. The period of the dashing is constant, what changes is dash/space ratio. This produces something like this:

enter image description here

However in that image the dashing has a uniform origin and this creates unsightly vertical gutters. So I tried to randomize the origin to remove the gutters. This sort of works but there is an obvious pattern:

enter image description here

Wondering where this comes from I made a very simple test case with stacked dashed straight lines:

  • dash ratio: 50%
  • dash period 20px
  • origin shift from -10px to +10px using random.uniform(-10.,+10.)(*) (after an initial random.seed()

enter image description here

And with added randomness:

enter image description here

So there is still pattern. What I don't understand is that to get a visible gutter you need to have 6 or 7 consecutive values falling in the same range (says, half the total range), which should be a 1/64 probability but seems to happen a lot more often in the 200 lines generated.

Am I misunderstanding something? Is it just our human brain which is seeing patterns where there is none? Could there be a better way to generate something more "visually random" (python 2.7, and preferably without installing anything)?

(*) partial pixels are valid in that context