-2

Before I reinvent the wheel, I wanted to find out if there is an algorithm that randomly divides an amount of money into bills and coins.

e.g. I have an amount of 125.50€. possible 1st division:

100 € = 1
50 € = 0
20 € = 1
10 € = 0
5 € = 1
2 € = 0
1 € = 0
0,5 € = 0
0,2 € = 2
01 € = 1

possible 2nd division:

100 € = 0
50 € = 2
20 € = 1
10 € = 0
5 € = 1
2 € = 0
1 € = 0
0,5 € = 1
0,2 € = 0
01 € = 0

It is important that the distribution must be random. Can you give me some advice or is that too complicated?

thanks in advance for the help and advice.

Best regards

KenMasters
  • 401
  • 3
  • 8
  • 19
  • I need a better understanding. Do you just randomly select a coin from all possible coins smaller than the amount remaining and subtract it? Then repeat until the amount remaining is smaller than the smallest coin size? – Michael Foster Mar 22 '23 at 18:17
  • Hello Michael, thank in advance for your question. What i want is, that the result should just be different. For example, 100 can be splittet in 2x50 or (4x20 and 2x10) or (1x50 and 1x20 and 3x10). That's what I mean by random, since many variants are possible. – KenMasters Mar 22 '23 at 19:06

2 Answers2

0

Start as in your first example: You can begin with 0 or 1 100€ units. Chose randomly between 0 and 1. If you chose 0 you get 0..2 50€ units. Chose a random number between 0 and 2. Proceed in this manner until you end up at the 1 cent units.

Elec1
  • 235
  • 2
  • 8
0

Here is a VB.NET solution. Feel free to modify it as needed. For example, I'm not sure how you want the output.

Dim startAmount As Decimal = 125.5
Dim currentAmount As Decimal = startAmount
Dim money As Dictionary(Of Decimal, Integer) = ".01,.2,.5,1,2,5,10,20,50,100".Split(",").ToList().Select(Function(c) CType(c, Decimal)).ToList.ToDictionary(Of Decimal, Integer)(Function(m) m, Function(z) 0)
Dim rand As New Random
Do Until currentAmount < money.Min(Function(m) m.Key)
    Dim increment As Decimal = Decimal.MaxValue
    Do Until increment <= currentAmount
        increment = money.ElementAt(rand.Next(money.Count - 1)).Key
    Loop
    currentAmount -= increment
    money(increment) = money(increment) + 1
Loop
For Each denomination As KeyValuePair(Of Decimal, Integer) In money
    If denomination.Value > 0 Then Console.WriteLine(String.Format("{0} items of value {1}", denomination.Value, denomination.Key))
Next
Console.WriteLine(String.Format("This leaves {0} left over.", currentAmount))
Michael Foster
  • 420
  • 6
  • 12
  • Hello Michael. First of all, thank u very much for your help! I am getting an error in line 3 with the message: "Option Strict On" does not allow implicit string to char conversion. – KenMasters Mar 23 '23 at 10:42
  • Are you using C# instead of VB.NET? If so, then if you change `","` to `','` it sounds like it would fix that. Otherwise, you might have to break up the line into more than one, to get the comma as a `char` instead of a `string`. – Michael Foster Mar 23 '23 at 11:38
  • Alternatively, if you're actually using VB.NET, go to Tools-Options-Projects and Solutions-VB Defaults, and set Option Strict off, which would fix the message you're getting. – Michael Foster Mar 23 '23 at 12:07