flat assembler
Message board for the users of flat assembler.

 Index > Heap > numbers distribution question
Author
sleepsleep

Joined: 05 Oct 2006
Posts: 8864
Location: ˛　　　　　　　　　　　　　　　　　　　　　　　　　　　　　⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
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"
11 Feb 2018, 18:36
sleepsleep

Joined: 05 Oct 2006
Posts: 8864
Location: ˛　　　　　　　　　　　　　　　　　　　　　　　　　　　　　⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
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
]
```
12 Feb 2018, 02:17
Furs

Joined: 04 Mar 2016
Posts: 1466
Furs
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.
12 Feb 2018, 13:18
quirck

Joined: 18 Aug 2013
Posts: 5
quirck
Wouldn't this do?
Code:
`for (i = 0; i < 100; i++) days[random(30)]++;    `
14 Feb 2018, 05:30
sleepsleep

Joined: 05 Oct 2006
Posts: 8864
Location: ˛　　　　　　　　　　　　　　　　　　　　　　　　　　　　　⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
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);
}    ```
14 Feb 2018, 12:28
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 17247
revolution
Why no assembly code?
14 Feb 2018, 12:34
sleepsleep

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

why you speak english instead of mandarin?

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,
14 Feb 2018, 13:12
sleepsleep

Joined: 05 Oct 2006
Posts: 8864
Location: ˛　　　　　　　　　　　　　　　　　　　　　　　　　　　　　⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
i need some input how to calculate pyramid nodes? hierarchical pyramid nodes (those direct selling nodes) any idea?
14 Feb 2018, 13:23
Furs

Joined: 04 Mar 2016
Posts: 1466
Furs
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 )
14 Feb 2018, 15:23
sleepsleep

Joined: 05 Oct 2006
Posts: 8864
Location: ˛　　　　　　　　　　　　　　　　　　　　　　　　　　　　　⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣⁣Posts: 334455
sleepsleep
what Furs?
15 Feb 2018, 19:58
Furs

Joined: 04 Mar 2016
Posts: 1466
Furs
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.
16 Feb 2018, 13:55
quirck

Joined: 18 Aug 2013
Posts: 5
quirck
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.
16 Feb 2018, 15:49
Furs

Joined: 04 Mar 2016
Posts: 1466
Furs
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,

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).
16 Feb 2018, 18:28
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsCompiler InternalsIDE DevelopmentOS ConstructionNon-x86 architecturesHigh Level LanguagesProgramming Language DesignProjects and IdeasExamples and Tutorials Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou can attach files in this forumYou can download files in this forum