OK here it is. I have got table of numbers for exapmle: 1 3 4 5 3 2 1 9 5 7

now i want to randomly take position of one number. but possibility of taking position is greater if number in this position is smaller.

So it's more possible to random possition 0 or 6 (number 1) than position 1 or 5 (number 3) etc. etc.
Posted on 2004-11-14 13:14:09 by AceEmbler
Here's one solution, where the chance decreases linearly.
``````
Init:
push esi
push ebx
mov esi,Table
push esi
xor ebx,ebx
push Length
pop ecx
push ecx
a1:
lodsb
cmp bl,al
jae a2
mov bl,al
a2:
loop a1
pop ecx
pop esi
xor eax,eax
mov edx,ecx
a3:
lodsb
sub edx,eax
loop a3
pop ebx
pop esi
mov [Total],edx
mov [Max],bl
ret
Find:
call SomeRandomGenerator
push esi
push ebx
mov bl,[Max]
mov esi,Table
imul [Total]
xor ecx,ecx
xor eax,eax
dec eax
a4:
mov al,[Table+ecx]
sub al,bl
dec eax
inc ecx
jb a4
dec ecx
xchg ecx,eax
ret
Total dd ?
Max db ?
``````
Posted on 2004-11-14 13:58:36 by Sephiroth3
Im in need of example made in C/C++ so it is preaty useless for me, but thx anyway. Maby somebody knows some equation ???
Posted on 2004-11-14 14:23:06 by AceEmbler
Homework is it? You should really know by now that non asm questions should be clearly described as such and put in The Heap.

Here's an algorithim in words:

``````Invert the numbers, ie 1 becomes 9, 2 becomes 8,... (I do this cause its easier if the bigger number is more probable)

So the table becomes 9 7 6 5 7 8 9 1 5 3

Sum all the values, giving 60 (quick addition could be wrong :) )

Now genetate a random number between 1 and 60 call it x.

Scan across table subtracting the values from x stopping when x is <= 0

Where you stop is the position chosen.``````

This method mean the 1 stops are 9 times more likly to be chosen than the 9 spots. This is propably similar to what Sephiroth3's code uses.
Posted on 2004-11-14 15:16:15 by Eóin

It was easy i should take a break.
Posted on 2004-11-14 16:11:08 by AceEmbler
A "Thank you for your assistance" would've been nicer.
Posted on 2004-12-01 08:03:57 by sh10453
A "Thank you for your assistance" would've been nicer.
Posted on 2004-12-01 08:28:50 by sh10453
A "Thank you for your assistance" would've been nicer.
Posted on 2004-12-01 08:29:18 by sh10453
A "Thank you for your assistance" would've been nicer.
Posted on 2004-12-01 08:36:12 by sh10453
I guess he is at sleep lol....
Posted on 2004-12-01 09:05:54 by rea
; this is kind of fun and non-linear...

xor ebx, ebx
mov esi, ArrayUInt32
xor ecx, ecx

TryAgain:

invoke Random, Min, Max ; preserves ECX, EBX, ESI
mov eax,
inc ecx