lundi 24 février 2020

Expected expression before "double" when using rand()?

Despite reading similar questions with the same topic, I seem to not get the reason my compiler is telling me "Expected expression before double".

Down there in my code, I am trying to create a program which generates a lattice, then the atoms will move in random velocities after performing a Box-Muller transformation. The new positions will then be recorded and exported into a .txt file, which will then be modified for VMD to read:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define Kb 8.6173324e-5 //Boltzmann constant (in eV/K)
#define PI 3.14159265
#define C 299792.458  // in angstrom/ps
#define Temp 273.15 // in K
#define mass 3.350918e-26 //in KG

//this is the algorithm for Box-Muller transformation
double Box_Muller_Transformation(double box1, double box2)
{
//box1 and box2 are independent, random numbers randing from 0 to 1, the calculated result is num1.
double num1 = sqrt(-2*log(box1))*cos(2*PI*box2);
//returning result for future calling
return num1;
}

int a = 9, b = 2; //this is the lattice size, boundary is set as 100 in each direction
double t=0, num1=0, num2=0;
double box1 = double(rand()/RAND_MAX); //generating box1
double box2 = double(rand()/RAND_MAX); //generating box2

int main()
{
    //generating the 9 particles as a 2x2 lattice
    // making 2x2 array
    int i=0, j=0;
    int position[9][2];

    //i is x-coordinate of particle
    //j is y-coordinate of particle
    //k is z-coordinate of particle
    //a is the number of particle


    FILE *fp;
    fp = fopen ("lab2testing.txt", "w+"); //preparing a file to write in stuff

    for (i=0, a=0; i<9, a<9; i++, a++)
    {
            position[a][0]=i;
            position[a][1]=j;
            if(a==2||a==5)
            {
                i=0;
                j++;    
            }
            printf("%d,%d\n", position[a][0], position[a][1]);
    }

    //calculating the path of the article

    for (a=0, t=0; a<9, t=100; a++, t=t+0.01)
    {
        //calculating the new positions through euler's method

        num1= Box_Muller_Transformation(box1, box2);

        num2 = num1*sqrt(Kb*Temp/mass);

        position[a][0]=position[a][0]+0.01*num2;

        position[a][1]=position[a][1]+0.01*num2;

        //4 scenarios of getting out of bounds

        if (position[a][0]>50)
        {
            num2=-num2;
            position[a][0]=position[a][0]-0.01*num2;
        }
        if (position[a][0]<0)
        {
            num2=-num2;
            position[a][0]=-position[a][0];
        }
        if (position[a][1]>50)
        {
            num2=-num2;
            position[a][1]=position[a][1]-0.01*num2;
        }
        if (position[a][1]<0)
        {
            num2=-num2;
            position[a][1]=-position[a][1];
        }

        fprintf(fp, "%d \n%s \n", a, "particle");
        fprintf(fp, "%s %f %f\n ", "Ar", position[a][0], position[a][1]);
        printf("%f %f\n", position[a][0], position[a][1]);

    }

    return(0);
}

My problem is on line 21 and 22, the compiler said that it "expected expression before double" for "box1" and "box2", used for the transformation to get the velocity of the particles, but I do not know why is that.

double box1 = double(rand()/RAND_MAX); //generating box1
double box2 = double(rand()/RAND_MAX); //generating box2

The number types should be the same on both sides (double and double) and I planned to use that line to produce a number between 0 to 1 by letting the program generate a number, then dividing it by the largest number rand() can produce so it can theoretically produce a number from 0 to 1. However, I just don't understand why it won't compile. How should I modify my code to make it run?




How to create two different random color groups in NetLogo 6.1.1?

How to create two different random color groups in NetLogo 6.1.1?

I am trying to create two different groups from the group of 250 turtles. The starting situation is that all 250 turtles are gray and then they will turn to yellow and pink groups one by one.

This code of mine makes all turtles in the beginning to be gray and then they will all turn to pink. I do not want this but I want two randomly made groups where pink turtles is typically either larger or smaller group of turtles than the yellow group of turtles in the end of the code run.

I just started to code with NetLogo 6.1.1. Thank you for understanding and all help and have a nice day.

[
  time

  person
]


turtles-own [ x ]

to setup

  clear-all
  reset-ticks
  set time  0
  create-turtles 250 

  [
      setxy random-xcor random-ycor 

  ]  

  ask turtles

  [
    set shape "person"
    set size 1
    set color gray
  ]

  end


to go 

  ask turtles


 [

    show random 2 = x

    if x = 1 [set color yellow] 
    if x = 0 [set color pink]
  ]


end ```



How to get javascript to prompt a question a set number times

Im trying to write a simple program that asks the user to guess a random number a set number of times. So far I have the following:

<!DOCTYPE html>
<html>
<head>
    <title>Decisions and Loops</title>
</head>
<body>

</body>
<script>

var rNum = Math.ceil(Math.random() * 30);

var myInput = Number (prompt ("Please enter your Guess: "));


if (myInput == rNum) {
    alert("Good guess");
    } 
    else if (myInput >= rNum){
        alert((prompt ("Sorry, guess if too low, try again"));
    } 
    else {
    alert((prompt ("Sorry, guess if too high, try again"));
    }

</script>
</html>

Two issues 1. For some reason this is not working. 2. How do I get the program to iterate through the loops a number of times? Any assistance would be appreciated.




Extra unwanted result when generating random number [duplicate]

I wanted to create a random numerical password generator.So, I made one. It works correctly, just like the way i wanted it to work.But there is an extra word printed in the console.

def randgen():
    # import the 'random' module
    import random
    # create variables for each digit in the password
    no1 = 0
    no2 = 0
    no3 = 0
    no4 = 0
    no5 = 0
    no6 = 0
    no7 = 0
    no8 = 0
    no9 = 0
    no10 = 0
    # generate random number for each digit
    # in each iteration
    for x in range(10):
        if x == 0:
            no1 = random.randrange(0, 9, 1)
        elif x == 1:
            no2 = random.randrange(0, 9, 1)
        elif x == 2:
            no3 = random.randrange(0, 9, 1)
        elif x == 3:
            no4 = random.randrange(0, 9, 1)
        elif x == 4:
            no5 = random.randrange(0, 9, 1)
        elif x == 5:
            no6 = random.randrange(0, 9, 1)
        elif x == 6:
            no7 = random.randrange(0, 9, 1)
        elif x == 7:
            no8 = random.randrange(0, 9, 1)
        elif x == 8:
            no9 = random.randrange(0, 9, 1)
        elif x == 9:
            no10 = random.randrange(0, 9, 1)

    # print all the digits concatenated
    print(no1, no2, no3, no4, no5, no6, no7, no8, no9, no10)


# just a cherry on the cake
print("Type 'y' when ready to generate random password.")
print("Type anything else to cancel.")
if input() == "y":
    print(randgen())
else:
    pass

Result:

Type 'y' when ready to generate random password.
Type anything else to cancel.
y
2 2 7 1 3 1 6 3 0 7
None

There's the word 'None' printed.I don't want it to appear.Can anyone help?




Is there a way to randomize the order of sub loops in a main loop?

I have 6 different blocks (for-loops) that are part of a main for-loop. Each block, consisting of 6 randomly picked stimuli, is supposed to be presented twelve times. The presentation of the blocks should happen in a random order.

So far, I created a main loop around the six blocks that repeats twelve times and I planned to randomize the order of the blocks with PsychoPy's TrialHandler. However, this does not work the way it should and I have to figure out another way. I was thinking:

  1. I could make a list (of numbers or strings) and shuffle this list and then create if-conditions for the sub loops i.e. "if "Block1" is list[0], then the for-loop Block1 is initialized. Does something like that work? If it does, how do I implement it because, ideally, I want to generate the order of the sub loops with the list.

  2. The other idea concerns the TrialHandler and is thus specific to PsychoPy. I was wondering if it is possible to create an excel file containing the different blocks and add it to the conditions parameter of the main loop. If every block would be a row, PsychoPy would be able to randomize these. However, I am unsure if this works and how the blocks could be added to an excel file.

Does any of these solutions seem reasonable or is there another way to achieve a randomization?




R - How to select a random vector

I have 4 vectors that contain integers.

I want to perform calculations based on 2 of the vectors, selected randomly.

I tried creating a new vector containing all the vectors, but sample() only gives me the first element of each vector.

My vectors if it helps:

A <- c(4, 4, 4, 4, 0, 0)
B <- c(3, 3, 3, 3, 3, 3)
C <- c(6, 6, 2, 2, 2, 2)
D <- c(5, 5, 5, 1, 1, 1)

The output I wanted is for example: A, B or B, D or D, A etc.

A thousand thanks in advance!




Another question about random numbers in Haskell

I am trying to make a version of the Voltorb game from Pokemon Gold and Silver in Haskell. Now for generation of the board, I want to have a list of (l,r,v) triplets where l is the line, r is the row and v is the value of the field.

Values l and r are implemented with list comprehension since they should be the same every time. As for v though I can't find an option to implement it so that it is 0,1,2 or 3 "randomly" (I know that Haskell is purely functional and there is no true randomness, that's part of the reason why I struggle with this).

If anyone could help with this I'd be very grateful. If you could also maybe give a short explanation as to why that solution works, that would help me a lot.

My current implementation of l and r:

field n = [(l,r,v) | l <- [0..n], r <- [0..n]]