Hello.

i will like to generate four numbers at random, that will total 100
and assign each to a textbox.

thanks for your help.

What have you tried so far?
What I would do is make a function that returned a List of Integers.
Each member of the list would be a random number between 0 and 100 minus (the sum of the list).

Of course, you should loop one-less time than 4 (or whatever count) and add the missing value to the last element.

This example uses Linq to sum the contents of the list for the first n digits.
When the sum reaches 100, only 0 would be added.
As long as the value stays less than 100 (or whatever max), it will continue adding.
The last number will be the difference of the sum subtracted from 100.
Check out how this works:

Imports System.Collections.Generic
Imports System.Linq
Module Module1
   Function GetXRandomToMax(ByVal intCount As Integer, ByVal intValMax As Integer) As List(Of Integer)
      Dim lst_intRetVal As New List(Of Integer)
      Dim rnd As New Random(DateTime.Now.Ticks Mod Integer.MaxValue)
      Dim intHolder As Integer = 0

      For i As Integer = 1 To (intCount - 1)
         intHolder = rnd.Next(0, intValMax - lst_intRetVal.Sum())
         lst_intRetVal.Add(intHolder)
      Next

      lst_intRetVal.Add(intValMax - lst_intRetVal.Sum())

      Return lst_intRetVal
   End Function

   Sub Main()
      Dim lst As List(Of Integer) = GetXRandomToMax(4, 100)
      lst.ForEach(Sub(i) System.Diagnostics.Debug.WriteLine(i))
   End Sub
End Module

My suspicions are you will need to re-write the .Sum() because you won't be allowed to use Linq. Also, you will need to incorporate the function and take the values and populate your text boxes instead of this that writes out to the Debug window.

Hello.

i will like to generate four numbers at random, that will total 100
and assign each to a textbox.

thanks for your help.

try this, can take up to 15 seconds before right combination is found,
but it works.

Public Class Form1
Dim number1 As Integer = 0, number2 As Integer = 0, number3 As Integer = 0, number4 As Integer = 0

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Timer1.Start() 'timer interval = 1
End Sub

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
'set random numbers
number1 = (100 * Rnd())
number2 = (100 * Rnd())
number3 = (100 * Rnd())
number4 = (100 * Rnd())
If number1 + number2 + number3 + number4 = 100 Then
TextBox1.Text = number1 & " " & number2 & " " & number3 & " " & number4
Timer1.Stop()
number1 = 0
number2 = 0
number3 = 0
number4 = 0
Timer1.Start()
End If
End Sub
End Class

thanks for you help. below is what am trying to do. Though the generated four numbers is more than 100. how to i make the total of the four numbers 100

Dim total As Decimal
        Dim data(4) As Decimal
        Dim percentage(4) As Decimal
        Dim number1 As Integer = 0, number2 As Integer = 0, number3 As Integer = 0, number4 As Integer = 0
        'set random numbers
        number1 = (100 * Rnd())
        number2 = (100 * Rnd())
        number3 = (100 * Rnd())
        number4 = (100 * Rnd())
        
        data(0) = number1
        data(1) = number2
        data(2) = number3
        data(3) = number4


        total = data(0) + data(1) + data(2) + data(3)
        percentage(0) = data(0) / total
        percentage(1) = data(1) / total
        percentage(2) = data(2) / total
        percentage(3) = data(3) / total

trying to draw a bar chat with the data. four bars

This is a simple code, that creates an array of 4 integers, and addes a random number to each index.

Dim r As New Random()
Dim nums As Integer() = New Integer(3) {0, 0, 0, 0}
For i As Integer = 0 To 3
	If nums.Sum() <= 100 Then
		nums(i) = r.[Next](1, 101)
	Else
		nums(i - 1) = 0
		nums(i - 1) = 100 - nums.Sum()
		Exit For
	End If
Next
If nums.Sum() < 100 Then
	nums(3) = 100 - (nums(0) + nums(1) + nums(2))
End If
commented: Execellent +1

How do i assign the generated random nunber to the data value

great got it.. thanks

data(0) = nums(0)
        data(1) = nums(1)
        data(2) = nums(2)
        data(3) = nums(3)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.