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!
 

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
 

Forum statistics

Threads
1,085,429
Messages
5,383,629
Members
401,843
Latest member
stevensmith1

Some videos you may like

This Week's Hot Topics

Top