Generate Random numbers with out using predefined functions

bglanton

New Member
Joined
Oct 3, 2014
Messages
10
  1. Ask the user for a positive integer (num), below 100.
  2. If the user enters a negative number or greater than 100, give a warning and ask to re-enter.
  3. Generate 20 positive random numbers between (1- num).
  4. Display the generated random numbers in the range (A1, A20).


I know there are random number generator functions in excel, but is there a way to do this without predefined functions in vba?

Thanks so much for any help!
 

Some videos you may like

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

adam087

Well-known Member
Joined
Jun 7, 2010
Messages
1,356
Kinda depends on your definition of random. Which is a real cop out of an answer.
Is there a reason you don't want to use the Excel functions?
/AJ
 

bglanton

New Member
Joined
Oct 3, 2014
Messages
10
Kinda depends on your definition of random. Which is a real cop out of an answer.
Is there a reason you don't want to use the Excel functions?
/AJ


You can can use the rnd vba function just not predefined excel functions per my instructor for the assignment
 

adam087

Well-known Member
Joined
Jun 7, 2010
Messages
1,356
Ah! OK well without doing your assignment for you (sorry). I'd look in to creating a new Function (a UDF) which can employ the RND VBA code, and perhaps introduce another hint of randomness by using some element of the current time.

You could use MOD to ensure you got a number below the predefined maximum. Although there are other ways.

/AJ
 

bglanton

New Member
Joined
Oct 3, 2014
Messages
10
Sub exercise2()
Dim num_input As Integer
Dim R As Integer

num_input = InputBox("Please enter an integer number less than 100")

If num_input < 0 Or num_input > 100 Then
num_input = InputBox("Warning, please enter an integer number less than 100")
End If
If 0 < num_input < 100 Then
Do While R = 20
For i = 1 To num_input
R = Int((num_input - i + 1) * Rnd() + i)
Next i
Loop

MsgBox (R)

End If

End Sub

I'm getting stuff on how to generate 20 random integers. I want the loop to run until there are 20 values
 

Watch MrExcel Video

Forum statistics

Threads
1,122,964
Messages
5,599,069
Members
414,281
Latest member
Engjamal2021

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top