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.

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.

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

add edx,ebx

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

add edx,eax

inc ecx

jb a4

dec ecx

xchg ecx,eax

ret

Total dd ?

Max db ?

Im in need of example made in C/C++ so it is preaty useless for me, but thx anyway. Maby somebody knows some equation ???

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:

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.

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.

Already made it, simmilar method as yours.

It was easy i should take a break.

It was easy i should take a break.

A "Thank you for your assistance" would've been nicer.

A "Thank you for your assistance" would've been nicer.

A "Thank you for your assistance" would've been nicer.

A "Thank you for your assistance" would've been nicer.

I guess he is at sleep lol....

; 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

add ebx, eax

mov edx, ebx

shr edx, cl

jne TryAgain

; EAX is returned value...

xor ebx, ebx

mov esi, ArrayUInt32

xor ecx, ecx

TryAgain:

invoke Random, Min, Max ; preserves ECX, EBX, ESI

mov eax,

inc ecx

add ebx, eax

mov edx, ebx

shr edx, cl

jne TryAgain

; EAX is returned value...