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!
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,394
Members
448,957
Latest member
Hat4Life

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
Back
Top