lundi 1 mai 2017

Random word generator with a equal probability of occurrence of letters

So I made a pretty simple word generator program in c# that works relatively well. My question is how to generate a words with a equal probability of occurrence of letters eg. aaaa, aabb or abab

My code:

listView1.Items.Clear();

        int num_letters = 4;
        int num_words = 20;

      char[] letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();

       Random rand = new Random();

         for (int i = 1; i <= num_words; i++)
        {
           string word = "";
            for (int j = 1; j <= num_letters; j++)
            {
                int letter_num = rand.Next(0, letters.Length - 1);

               word += letters[letter_num];
            }

             listView1.Items.Add(word);




How can I assign a random word from a wordlist to a string every 30 minutes in Android?

I'm learning Android, but I can't figure it out, some help would be appreciated. Thank you in advance.




How do i pick Random Value in this JSON structure?

I'm trying to get random Song from JSON which is sorted with Feeling/Genre/Artist/Songs this is my json file

// music_link.json

{
"Calm" : {
  "Jazz" : {
     "Limes" : {
            "Hooplah" : "http://ift.tt/2gor4KQ",
            "Blase" : "http://ift.tt/2oYb9C5",
            "Selfless" : "http://ift.tt/2pADySt"
  },
     "DemiCat" : {
            "Swingin' with a cat" : "http://ift.tt/2oY8LLj"
  },
     "Glen Check" : {
            "I've got this feeling" : "http://ift.tt/2pAnvnO",
            "Me & U (Remix Version)" : "http://ift.tt/2oY7Dr2"
  }
}
},

"Excite" :{
   "EDM" : {
     "MonsterCat" : {
       "Feint" : {
            "Snake Eyes" : "http://ift.tt/2pAzotU",
            "Arrowhead" : "http://ift.tt/2kDuElL"
    },
       "Hyper Potions": {
            "Surf" : "http://ift.tt/2kEIGRa",
            "Checkpoint" : "http://ift.tt/1OSXuIr",
            "Matches" : "http://ift.tt/2fXTr1t"
    }
  }
}
},

"Tired" : {
   "Beat" : {
     "Vibez" : {
            "Another Rainy Day" : "http://ift.tt/2gmld4I"
  },
     "Taiko" : {
            "Reflection" : "http://ift.tt/2pAAupE"
  }
}}}

By using for, I want to get the URL in Each Songs Randomly.

Do i need to make list for each values of Feeling/Genre/Artist/Songs ?

Or should i fix the sorting of the data? (I'm kinda new for JSON..Sorry)




Select Random Items but with some items having a higher percentage of being selected?

I'm using MVC C# and SQL Sever.

I have about 100 items that I have been selecting 2 random items from. This was straight forward sorting by a new guid.

 vm = vm.OrderBy(c => Guid.NewGuid()).Take(2).ToList();

But now I want to be able to mark a subset of items to have a higher percentage (20%) of being selected and then at a later date another subset to be 50%.

The list of items is static. It will always be about 100 items.

I was just going to brute force it and add 20% more of the selected items to a list and them just do the random selection twice.

vm1 = vm.OrderBy(c => Guid.NewGuid()).Take(1).Single();
vm2 = vm.OrderBy(c => Guid.NewGuid()).Where(_ => _.Id != firstSelectedID).Take(1).Single();

But was thinking there might be a cleaner more performant way in SQL or something that I'm missing in C#




check if number exists in subdocument with Mongoose

I have a document which looks like:

var User = new Schema({
    _id: ObjectId,
    name: String,
    position: [{
              _id:ObjectId,
              index_number:Number,
                    preference:[{
                        candidate_id: String,
                        weightedScore:Number,
                        position_id:String,
                        candidate_index_number:Number
                    }],

When I add a position, I generate an index_number using var index_number = Math.floor(100000 + Math.random() * 900000);

But I need to check if this number exists in the subdocument for any other position before I add it. So i need to check the number, and run the function again if number does exist, until i get a number that doesn't.. But this function doesn't go into the sub document..

 User.count({index_number: index_number}, function (err, count){
      if(count>0){
            console.log("no");
      }
      else{
          console.log("okay");
      }

how do I loop through the function and check the sub documents for the index_number? or is there a way that I am unaware of that allows me to insert a unique number that isn't an object Id with Mongoose?




I need help in C++ to generate random 0s and 1s accoding to the user input

I need help in C++ to generate random 0s and 1s according to the user input. If user inputs 70% that means 70% of the total outputs will be 1s and rest 30% will be 0s. Thanks in Advance.




Create 100 data.frames with dates grouped randomly with a fixed number of days

Here an example of my data.frame and let's assume that the date col represents dates:

df = read.table(text = 'ID date
a 1
a 2
a 3
a 4
a 7
a 11
b 1
b 3
b 4
b 10
b 11', header = TRUE)

So, my dates here range from 1 to 11 and I would like to create 100 data.frames where for each one the date (and ID) col will be grouped randomly by 3 subsequent dates.

e.g.

df1

ID date group
    a 1 1
    a 2 1
    a 3 1
    a 4 2
    a 7 2
    a 11 2
    b 1 3
    b 3 3
    b 4 3
    b 10 4
    b 11 4

df2

ID date group
    a 1 4
    a 2 1
    a 3 1
    a 4 1
    a 7 2
    a 11 2
    b 1 2
    b 3 3
    b 4 3
    b 10 3
    b 11 4

df3

ID date group
    a 1 3
    a 2 3
    a 3 3
    a 4 4
    a 7 4
    a 11 1
    b 1 1
    b 3 1
    b 4 2
    b 10 2
    b 11 2

etc...

Do you have any suggestion? Thanks