flat assembler
Message board for the users of flat assembler.

flat assembler > Heap > numbers distribution question

Author
Thread Post new topic Reply to topic
sleepsleep



Joined: 05 Oct 2006
Posts: 7395
Location: ˛                              ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣ Posts: 6699
let say i got a number, 100
i need to distribute it into 30 days,

100/30 and i have a uniform value in each day that eventually total up to 100.

how to have a non-uniform distribution that total up to 100?
eg:
day 1 = 4
day 2 = 1
day 3 = 0 and so on,

i probably could use random 0 to 10 (or day max value), assign the value to day 1, 100 minus day 1 and repeat random,

but i would like to see how you guys tackle such "idea"
Post 11 Feb 2018, 18:36
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 7395
Location: ˛                              ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣ Posts: 6699
i did one,
Code:
function distribute_algo( v, b ) {
        var va = eval(v);
        
        for( i=1; i< va.length; i++ ) {
                
                var r = Math.random();
                
                if( (b != 0) && (r > 0.5) ) {
                        va[i]++;
                        b--;
                }
                
        }
        return b;
}
    


usage
Code:
while( memtotaldistribute > 0 ) {
                memtotaldistribute = distribute_algo( "memregdaycount", memtotaldistribute );
        }
    


result is like
Code:
[
  null,
  11,
  9,
  6,
  7,
  13,
  14,
  14,
  12,
  8,
  9,
  16,
  11,
  13,
  11,
  14,
  12,
  13,
  13,
  11,
  11,
  12,
  13,
  10,
  6,
  12,
  9,
  11,
  6
]
    
Post 12 Feb 2018, 02:17
View user's profile Send private message Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 1201
Don't forget to shuffle the result at the end, otherwise it's far more likely to have larger numbers at the beginning of the list than at the end of it using your algorithm.
Post 12 Feb 2018, 13:18
View user's profile Send private message Reply with quote
quirck



Joined: 18 Aug 2013
Posts: 5
Wouldn't this do?
Code:
for (i = 0; i < 100; i++) days[random(30)]++;    
Post 14 Feb 2018, 05:30
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 7395
Location: ˛                              ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣ Posts: 6699
quirck wrote:
Wouldn't this do?
Code:
for (i = 0; i < 100; i++) days[random(30)]++;    


yah, it works too,
Code:
function callmemaybe() {
        var r = new Array();
        
        for( i=0; i<30; i++) r[i] = 0;

        for( i=0; i<100; i++) {
                var id = Math.floor( Math.random() * 30 );
                r[id]++;
        }
        console.log(r);
}    
Post 14 Feb 2018, 12:28
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 15904
Location: SDSS J140821.67+025733.2
Why no assembly code? Sad
Post 14 Feb 2018, 12:34
View user's profile Send private message Visit poster's website Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 7395
Location: ˛                              ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣ Posts: 6699
most likely, language is a limitation in constructing similar solution?

why you speak english instead of mandarin? Embarassed

personally i think, the gist is on the how to do it, instead of using what to do it, correct me if i am wrong,
Post 14 Feb 2018, 13:12
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 7395
Location: ˛                              ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣ Posts: 6699
i need some input how to calculate pyramid nodes? hierarchical pyramid nodes (those direct selling nodes) any idea?
Post 14 Feb 2018, 13:23
View user's profile Send private message Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 1201
sleepsleep wrote:
quirck wrote:
Wouldn't this do?
Code:
for (i = 0; i < 100; i++) days[random(30)]++;    


yah, it works too,
Code:
function callmemaybe() {
        var r = new Array();
        
        for( i=0; i<30; i++) r[i] = 0;

        for( i=0; i<100; i++) {
                var id = Math.floor( Math.random() * 30 );
                r[id]++;
        }
        console.log(r);
}    
But that's less efficient since 100 is more than 3 times larger than 30. Even with a shuffle, you'd only call the random function 60 times with the original idea + shuffle, not 100 like with this one. (and you should be using a good random function, therefore likely the bottleneck)

(I'm talking about algorithm complexity here, not micro optimizations, so it applies just fine to JS Wink)
Post 14 Feb 2018, 15:23
View user's profile Send private message Reply with quote
sleepsleep



Joined: 05 Oct 2006
Posts: 7395
Location: ˛                              ⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣ Posts: 6699
what Furs?
elaborate please, Laughing
Post 15 Feb 2018, 19:58
View user's profile Send private message Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 1201
Your first method loops 30 times and calls the random function 30 times, but it generates poor random distribution. To make it better, you can shuffle after it (which requires another 30 iterations and random calls). So your first method + a shuffle would have 30+30 = 60 calls to the random function.

The second method loops 100 times and calls the random function 100 times.

Needless to say, 60 is smaller than 100 by a quite large margin (almost half of it). So your first method + a shuffle is probably faster than quirck's method because it calls the random function 60 times instead of 100 times.
Post 16 Feb 2018, 13:55
View user's profile Send private message Reply with quote
quirck



Joined: 18 Aug 2013
Posts: 5
Furs, is this the first method you refer to?
sleepsleep wrote:

Code:
function distribute_algo( v, b ) {
        var va = eval(v);
        
        for( i=1; i< va.length; i++ ) {
                
                var r = Math.random();
                
                if( (b != 0) && (r > 0.5) ) {
                        va[i]++;
                        b--;
                }
                
        }
        return b;
}
while( memtotaldistribute > 0 ) {
                memtotaldistribute = distribute_algo( "memregdaycount", memtotaldistribute );
        }
    



If I'm not mistaken, this code doesn't call random only 30 times.
The idea is: repeatedly looping through the array, on each iteration randomly decide whether the current element should be incremented or not. Repeat until we have sum of elements equal to 100. So 100 is the minimum number of calls to random when always r > 0.5.

If you are worried about number of calls to random, compare it to call to random(900) and dividing it by 30 to get random quotient and random remainder.
Post 16 Feb 2018, 15:49
View user's profile Send private message Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 1201
No I meant this one:
sleepsleep wrote:
i probably could use random 0 to 10 (or day max value), assign the value to day 1, 100 minus day 1 and repeat random,
Wink

Of course, when doing the randomness in this way, you need a non-uniform distribution (gaussian) centered around 1 or so (so day 1 being 100 being extremely unlikely, which would render the others zero).

You can use an approximate function instead of gaussian since it's easier to compute etc but you might have to use floats and stuff then (random between 0-1 float, then map it to 0-100 with a curve that approximates gaussian, just a simple function nothing fancy).
Post 16 Feb 2018, 18:28
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Copyright © 1999-2018, Tomasz Grysztar.

Powered by rwasa.