Random Number Generator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • coreyover4
    New Member
    • Sep 2007
    • 6

    Random Number Generator

    I am trying to write a program where I ask the person to input the number of random numbers generated 1-100. Right now I am stuck on one point in particular. How do I write the program to only generate the number from the input box. I know it is probably something like adding value to the code but I am not quite sure.
    This is what I have right now and it creates code 1-100. How would I change this to only generating the number instead of actually always creating 100 random numbers.

    [CODE=vb]Public Sub Random()
    Dim X As Integer, Number As Integer
    Number = InputBox("Enter the number of random numbers to be generated between 1 and 100", "Random Numbers")
    X = 1
    While X <= 100
    Sheet1.Cells(X, 1) = Round(1 + Rnd() * 100, 0)
    X = X + 1
    Wend
    End Sub[/CODE]
    Last edited by Killer42; Sep 28 '07, 12:14 AM. Reason: Added CODE tag
  • Ali Rizwan
    Banned
    Contributor
    • Aug 2007
    • 931

    #2
    Hello
    Ok now use my code

    [CODE=vb]Dim intPosition As Integer
    Randomize
    intPosition = Int((1000 - 1 + 1) * Rnd + 1)
    lblP.Caption = intPosition[/CODE]

    If you write this code in a timer control then it'll generate a random number after a given interval.
    Generated numbers between 1-1000

    You can also modify by changing 1000 to val(text1) ok
    lblP is a label
    GOODLUCK

    Comment

    • coreyover4
      New Member
      • Sep 2007
      • 6

      #3
      I am trying that but its not working first of all and second of all I am supposed to use a while\wend loop to run the random numbers. I know I can't have yall do the work for me, I just need to know how to fix the code.

      Comment

      • coreyover4
        New Member
        • Sep 2007
        • 6

        #4
        Also how do I right the code to average the random numbers. Is it like I know for some part its Average = Round(Sum / Number, 0), but how would I define the sum?

        Comment

        • coreyover4
          New Member
          • Sep 2007
          • 6

          #5
          Ok I figured out the random number now I just have problems with the average. I have this so far...
          [CODE=vb]Average = Round(Sum / Number, 0)
          Range("c1").Val ue = "Average="
          Range("d1").Val ue = Average
          For Count = 1 To Number
          If Compare(Sheet1. Cells(Count, 1), Average) = 1 Then
          Sheet1.Cells(Co unt, 5) = "The Value " & Count & " Above the Average"
          ElseIf Compare(Sheet1. Cells(Count, 1), Average) = 2 Then
          Sheet1.Cells(Co unt, 5) = "The Value " & Count & " Equals the Average"
          Else
          Sheet1.Cells(Co unt, 5) = "The Value " & Count & " Below the Average"
          End If[/CODE]
          I think it's the sum part that I am having problems with. Also the count is wrong but I don't know what to put value there. I want it to be the number generated in the random number but how do I do that?

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            You shouldn't use sum as a variable name, as Sum() is the name of a function in Excel. (I assume this is Excel - it sure loos like it.)

            Comment

            Working...