jeudi 1 septembre 2016

Randomly generate name from Array with zero repeat?

Update: The difference is I'm not trying to make one list I'm trying to make a button that can be clicked and generate a random name

Goal: Click a button and randomly generate a name from an array. I'm trying to be able to click the button and show one name randomly at a time with no repeating names. So far I've been able to randomly select a name but the names still repeat. How could I change my code to avoid any repeating names?

 $( ".next").click(function() {
     $(".intro").hide()
     var people = ["Andrew", "Adam", "Seth", "Mattos", "Eric"];
    for(i=0;i<1;i++){
    var randomPosition = Math.floor(Math.random() * people.length);
    var selected = people.splice(randomPosition,1);
    console.log(selected)

    $('#person').text(selected)


    if ($('#person').text() === "Mattos"){
        $("#selectedPerson").text("Mattos")
    }
    if ($('#person').text() === "Andrew"){
        $("#selectedPerson").text("Andrew")
    }
    if ($('#person').text() === "Eric"){
        $("#selectedPerson").text("Eric")
    }

    if ($('#person').text() === "Seth"){
        $("#selectedPerson").text("Seth")
    }
    if ($('#person').text() === "Adam"){
        $("#selectedPerson").text("Adam")
    }
    }
    });




Rails 4: using offset on a randomized list produces unexpected results

...or maybe, i just don't get it.

TL;DR - i get totally randomized results with duplicates and such. No idea why.

Here's the problem - i'm trying to make a rails app generating tournament brackets. The user can select whether he'd like to randomize the team list when generating the bracket, or later assign the teams by himself.

If i choose the latter option and generate the bracket as the ids go, everything is fine. If i choose to randomize the list, the list itself is fine, but later when assigning teams to particular matches (in a loop), the resulst are completely unorganized and unexpected (at least to me) eg.

instead of
Match 1: Team 1 vs Team 2
Match 2: Team 3 vs Team 4
etc. (as in the normally generated list)

i get something completely random with frequent duplicates like:
Match 1: Team 1 vs Team 1
Match 2: Team 1 vs Team 9

if @seed = 'random'
  @team_list = @tournament.teams.order("RAND()")
else
  @team_list = @tournament.teams.order(:id)
end

Here's how i assign the teams. The thing that screws everything up seems to be the "offset" part, and i don't get it, why.

@match.team_a = @team_list.offset((2*match_number)-2).limit(1).first               
@match.team_b = @team_list.offset((2*match_number)-1).limit(1).first




Random number generator that can use output to get seed

Basically I need a way to generate a random number using a seed, but if I put that output back in it will give me the original seed.

For example if I used 234 as the seed and it returned 9999, I want to be able to put 9999 into it and get the original seed (234).

Is there any way to do this?




How do I change the colors of 9 objects after the right object is pressed?

I have 9 buttons/squares that each have a different color. When the right color is pressed, all the buttons should switch colors. For example: if I press the red square, then the colors of the squares should change so that another square becomes red. So far I got

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    var Ctimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "colors", userInfo: nil, repeats: true)

    B1.backgroundColor = UIColor.blueColor()
    B2.backgroundColor = UIColor.greenColor()
    B3.backgroundColor = UIColor.cyanColor()
    B4.backgroundColor = UIColor.purpleColor()
    B5.backgroundColor = UIColor.redColor()
    B6.backgroundColor = UIColor.brownColor()
    B7.backgroundColor = UIColor.yellowColor()
    B8.backgroundColor = UIColor.orangeColor()
    B9.backgroundColor = UIColor.magentaColor()
}

func colors() {
    var red = CGFloat(drand48())
    var green = CGFloat(drand48())
    var blue = CGFloat(drand48())
    self.view.backgroundColor = UIColor(red: red, green: green, blue: blue, alpha: 1.0)
}

func changeButtonColors(){

    var rand = arc4random_uniform(10)


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

B1, B2, etc are buttons. I am confused as to how I can randomize the change of colors without having to hardcode every possible alternative.




Postgres doesn't use index

I am using postgres 9.5 on linux7. Here is the environment:

create table t1(c1 int primary key, c2 varchar(100));

insert some rows in just created table

do $$
begin
for i in 1..12000000 loop
insert into t1 values(i,to_char(i,'9999999'));
end loop;
end $$;

Now I want to update c2 column where c1=random value (EXPLAIN show that index is not used).

explain update t1 set c2=to_char(4,'9999999') where c1=cast(floor(random()*100000) as int);
                                    QUERY PLAN                                    
----------------------------------------------------------------------------------
 Update on t1  (cost=10000000000.00..10000000017.20 rows=1 width=10)
   ->  Seq Scan on t1  (cost=10000000000.00..10000000017.20 rows=1 width=10)
         Filter: (c1 = (floor((random() * '100000'::double precision)))::integer)
(3 rows)

Now, if I replace "cast(floor(random()*100000) as int)" with a number (any number) index is used:

explain update t1 set c2=to_char(4,'9999999') where c1=12345;
                               QUERY PLAN                                
-------------------------------------------------------------------------
 Update on t1  (cost=0.15..8.17 rows=1 width=10)
   ->  Index Scan using t1_pkey on t1  (cost=0.15..8.17 rows=1 width=10)
         Index Cond: (c1 = 12345)
(3 rows)

Questions are:

  1. Why in first case (when random() is used) postgres doesn't use index?
  2. How can I force Postgres to use index?



Find Sample Data for Application Portfolio

I don't know either this is the right place to ask this question or not but i will try.

I want to find some sample data where i will find information about ---

a organization's

Application
Lifecykel
Projects and information
Risk
Personal
Orzanizations
Vendor
Position
Response and so on ...

I have search a lot to find some sample data which can cover those fields within an organization.

Do anyone knows any resources to find some sample data's!

Thanks a lot in Advance...




Using Numpy's random.choice without replacement

According to the http://ift.tt/1Dult80, using the replace = False with Numpy's random.choice method should make the sample without replacement. However, this does not seem to work for me:

In [33]: import numpy as np

In [34]: arr = range(5)

In [35]: number = np.random.choice(arr, replace = False)

In [36]: arr
Out[36]: [0, 1, 2, 3, 4]

The array arr is still range(5) after sampling, and not missing a (random) number as I would expect. How could I sample a number from range(5) without replacement?