dimanche 23 février 2020

How can I generate random points faster in a large arrays?

I am trying to generate random points inside a 3d pepper like shape however when the arr_size is large it takes too long to generate these points.

arr_size is (30,30,30) it takes very little time to generate 1000 random point inside the 3d shape however when arr_size = (265,490,286) it takes a realy long time.

from matplotlib import pyplot as plt
import numpy as np


def create_bin_pepper(arr_size, center):
    coords = np.ogrid[:arr_size[0], :arr_size[1], :arr_size[2]]
    c = 10
    a1 = np.random.randint(low=5,high=10)
    b1 = np.random.randint(low=7,high=10)
    a2 = np.random.randint(low=5,high=10)
    b2 = np.random.randint(low=7,high=10)
    a3 = np.random.randint(low=5,high=10)
    b3 = np.random.randint(low=7,high=10)
    ellipse1 = ((np.square(coords[0] - center[0]))/np.square(a1) + (np.square(coords[1]-center[1]))/np.square(b1)  + (np.square(coords[2]-center[2]))/np.square(c)  <= 1)
    ellipse2 = ((np.square(coords[0] - center[0]-3))/np.square(a2) + (np.square(coords[1]-center[1]-5))/np.square(b2)  + (np.square(coords[2]-center[2]))/np.square(c)  <= 1)
    ellipse3 = ((np.square(coords[0] - center[0]+3))/np.square(a3) + (np.square(coords[1]-center[1]-5))/np.square(b3)  + (np.square(coords[2]-center[2]))/np.square(c)  <= 1)
    pepper = ellipse1|ellipse2|ellipse3
    pepper2 = np.where(pepper==1,230,pepper)

    for im in range(0,1000):
        #r2=1
        centre_x1 = np.random.randint(low=center[0]-a1+4,high=center[0]+a1-4)#low=11,high=20
        centre_y1 = np.random.randint(low=center[1]-b1+4,high=center[1]+b1-4)#low=15,high=23
        centre_z1 = np.random.randint(low=center[2]-c+4,high=center[2]+c-4)#low=10,high=20

        centre_x2 = np.random.randint(low=center[0]-a2+4,high=center[0]+a2-4)#low=11,high=20
        centre_y2 = np.random.randint(low=center[1]-b2+4,high=center[1]+b2-4)#low=15,high=23
        centre_z2 = np.random.randint(low=center[2]-c+4,high=center[2]+c-4)#low=10,high=20

        centre_x3 = np.random.randint(low=center[0]-a3+4,high=center[0]+a3-4)#low=11,high=20
        centre_y3 = np.random.randint(low=center[1]-b3+4,high=center[1]+b3-4)#low=15,high=23
        centre_z3 = np.random.randint(low=center[2]-c+4,high=center[2]+c-4)#low=10,high=20

        inside_ellipse1 = ((np.square(coords[0] - centre_x1))/np.square(a1) + (np.square(coords[1]-centre_y1))/np.square(b1)  + (np.square(coords[2]-centre_z1))/np.square(c)  <= (1/((np.square(a1))*(np.square(b1))*(np.square(c)))))
        inside_ellipse2 = ((np.square(coords[0] - centre_x2-3))/np.square(a2) + (np.square(coords[1]-centre_y2-5))/np.square(b2)  + (np.square(coords[2]-centre_z2))/np.square(c)  <= (1/((np.square(a2))*(np.square(b2))*(np.square(c)))))
        inside_ellipse3 = ((np.square(coords[0] - centre_x3+3))/np.square(a3) + (np.square(coords[1]-centre_y3-5))/np.square(b3)  + (np.square(coords[2]-centre_z3))/np.square(c)  <= (1/((np.square(a3))*(np.square(b3))*(np.square(c)))))

    pepper2 = inside_ellipse1 | inside_ellipse2 | inside_ellipse3 | pepper2
    pepper3 = np.where((pepper2!=230)&(pepper2!=0),160,pepper2)
    return pepper3

arr_size = (265,490,286)
sphere_center1 = (133,216,40)

pepper = create_bin_pepper(arr_size,sphere_center1)
axis = pepper[:,:,40]
plt.imshow(axis,cmap='gray')#,interpolation='bicubic'
plt.show()




java: method (in enum) to not to return same names [duplicate]

So basicly I need the getRandomNameCourseAsString() return name of subject, but I call it in for(..), so it can return same subject. I don't want to see same subject twice, so i tried this method but it gives me

java.lang.ArrayIndexOutOfBoundsException: 43

Commented lines is working if replace return

public enum CourseName {
    PHYSICS,
    CHEMISTRY,
    HISTORY,
    UKRAINIAN,
    ENGLSH,
    PROGRAMMING,
    PSYCHOLOGY,
    PHYLOSOPHY,
    OOP,
    COMPUTER_SCIENCE;
    private static final CourseName[] COURSE_NAMES = values();
    private static final int SIZE = COURSE_NAMES.length;
    private static final Random RANDOM = new Random();
    private static ArrayList<Integer> list = new ArrayList<>(SIZE);


    //can return same subject if: return COURSE_NAMES[RANDOM.nextInt(SIZE)];
    public static CourseName getRandomCourseName()  {
        return COURSE_NAMES[Rand(list)];
    }

    //want it to not return same subject by this method
    private static int Rand(ArrayList<Integer> list){
        for(int i = 1; i <= SIZE; i++) {
            list.add(i);
        }
        int index = RANDOM.nextInt(list.size());
        list.remove(index);
        return index;

    }
    public static String getRandomNameCourseAsString(){
        return String.valueOf(CourseName.getRandomCourseName());
    }
}



JavaScript color Array

I watched a codecademy video and have been unable to randomize my color array. The colors are not random. I am not sure what I am doing wrong.

    function getRandomColor() {
    var color;
    var colorArray = [
        "#FF6633",
        "#FFB399",
        "#FF33FF",
        "#FFFF99",
        "#00B3E6",
        "#E6B333",
        "#3366E6",
        "#999966",
        "#809980",
        "#E6FF80",
        "#1AFF33",
        "#999933",
        "#FF3380",
        "#CCCC00",
        "#66E64D",
        "#4D80CC",
        "#FF4D4D",
        "#99E6E6",
        "#6666FF"
    ];
    for (var i = 0; i < colorArray; i++) {
        color = colorArray[Math.floor(Math.random() * colorArray.length)];
    }
    return color;
}



How to select 11 best players from a list of players in python?

I am trying to create a program where I should be able to randomly select 11 players from 4 lists of players. Here is what I have tried.

import random
                                                                   # PLayers   Min   Max
                                                                   # ---------------------
wk = ['Rahul', 'Siefiet']                                          # wk         1     4
batsman = ['Rohit', 'Iyer', 'Munro', 'Pandey', 'Guptil', 'Taylor'] # batsman    3     6
bowler = ['Bumrah', 'Southee', 'Chahal', 'Sodhi', 'Dube']          # bowler     3     6
allRounder = ['jadeja', 'Santer', 'Dube']                          # allrounder 1     4
counter = 0

bat = random.randint(3, 6)
allr = random.randint(1, 4)
print(bat)

while counter != 11:
    for a in range(bat):
        print("Batsman:", set(random.choice(batsman)))
        counter += bat #adding random generated number to counter
    for a in range(bat):
        print("Bowler", set(random.choice(bowler)))
        counter += bat
    for a in range(allr):
        print("Allrounder:", set(random.choice(allRounder)))
        counter += allr
    for a in range(allr):
        print("Allrounder:", set(random.choice(wk)))
        counter += allr

When I try to run it. It goes to an infinite loop, Here is a screenshot.

enter image description here

Here is something I want to add to this.

if the randomly generated value for the batsman is 6 then 3 bowlers 1 all-rounder 1 wk
if the randomly generated value for the (batsman is 5) then (3 or 4 bowlers), (1 or 2 all-rounder depending upon if 3 bowlers are opted then 2 all rounders if 4 bowlers are opted then 1 allrounder), (1 or 2 wk depending upon if 2 AR are opted then 1 wk if 1 AR is opted then 2 wk)
And Many more conditions Like This

Any Idea how can I implement it???




samedi 22 février 2020

import random using Miniconda3 provides error

I'm learning Python and have installed miniconda3 to manage virtualenvs and packages.

I'm attempting a simple command: import random

This results in the following error:

 import : The term 'import' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the 
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ import random
+ ~~~~~~
    + CategoryInfo          : ObjectNotFound: (import:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

After searching through stackoverflow, reading Python/Anaconda documentation, it seems maybe my path is the problem however after reading Anaconda documentation it appears I should not override the standard path Anaconda provided during installation.

Thanks in advance for the help on this. Much appreciated.




Shuffle vector in C++14, C++17 [duplicate]

I am trying to shuffle my vector, but I am struggling to get it to work. I tried to look for answers online, but many answers seem to use a deprecated function std::random_shuffle. So far, my code looks like:

int main() {
    srand(time(NULL));

    vector<int> vec{1, 2, 3, 4, 5, 6};

    shuffle(vec.begin(), vec.end(), rand);

    return 0;
}

Could anyone tell me a concise and correct way to shuffle my vector?




Change chances of picking a random string

I would like Anna to have 67% chance of being picked randomly, Bob to have a 30% change, and Tom to have a 3% chance. Is there a simpler way to do this?

This is what I have so far:

var nomes = ['Anna', 'Bob', 'Tom'];
var name = nomes[Math.ceil(Math.random() * (nomes.length - 1))];
console.log(name);