You are viewing bpfurtado

stackoverflow debut

Yesterday I have created my account at StackOverFlow and answered my first 2 quest ions:

The last question I replied is basically about how to get random numbers (from a determined set) to be equally and randomly distributed to another set of objects. For instance, I have the codes {A, B, C} and I want to spread them equally to a collection of 3000 objects.

I need the Service_Types to be distributed uniformly in all the 30 tables. Similarly I need feedback value of 1 to be generated many times other than 0 and -1.

This is a simple Groovy code that does the trick (here I'm using 3 different codes to distribute among 99 objects):

   1 feedbacks = []
   2 3.times {
   3     feedback = it
   4     33.times {
   5         feedbacks << feedback
   6     }
   7 }
   8 
   9 rnd = new Random()
  10 
  11 idx = 0
  12 services = [:]
  13 99.times {
  14     services["Service_${idx++}"] =
  15         feedbacks.remove(rnd.nextInt(feedbacks.size()))
  16 }
  17 
  18 println Collections.frequency(services.values(), 0)
  19 println Collections.frequency(services.values(), 1)
  20 println Collections.frequency(services.values(), 2)
  21 
  22 println services

For the sake of simplicity I'm using a simple Map to represent the 99 objects, the key would be the object id and the value would be its code.



Comment Form

No HTML allowed in subject

  
 
   
 

Notice! This user has turned on the option that logs your IP address when posting. 

(will be screened)

Latest Month

January 2014
S M T W T F S
   1234
567891011
12131415161718
19202122232425
262728293031 

Tags

Powered by LiveJournal.com
Designed by Tiffany Chow