Random Number Generator - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Random Number Generator

SmikeySmikey Posts: 8Member
For a college project we are making a game which displays a random image from a file and shows this with 4 answer buttons and the image must be identified by the user.

There are 8 pictures in the game and the array the_pictures is set up accordingly.

Could anyone enlighten me on why the random number generator will sometimes generate the number 9 as before now it has not done so and all of a sudden its started generating 9 since i fiddled with the code.

[code]
Randomize
Do
RandomNumber = (Rnd * 8)
PictureDisplay.Picture = the_pictures(RandomNumber)
Loop Until RandomNumber <> 0
[/code]

Many Thanks in Advance

Comments

  • DaiMitnickDaiMitnick Posts: 77Member
    To be honest I can't see why from that code, it would give you number 9 sometimes. However, can you not simply change the loop until statement to bypass this oddity,

    i.e. [red] Loop Until RandomNumber <> 0 AND RandomNumber < 9 [/red]

    or something to that effect. Let me know if this is a stupid answer. Dai
    -----------------------------------------
    Do or do not, there is no try. |
    -----------------------------------------
  • SmikeySmikey Posts: 8Member
    Im afraid this didnt seem to work.

    Here is the entire program's code.

    [code]'Title: Fruit Game with Arrays
    'Written By: Michael Askew
    'Created using: Visual Basic 6

    'Version: 1.0
    'Start Date: 29th June 2009
    'End Date: -- ---- ----

    Option Explicit
    Dim RandomNumber As Integer
    Dim RandomPictures As String

    Private the_pictures() As StdPicture

    Private Sub Form_Load()
    Dim file_path As String
    Dim i As Integer
    ReDim the_pictures(1 To 8)

    file_path = App.Path
    If Right$(file_path, 1) <> "" Then file_path = file_path & ""
    For i = 1 To 8
    Set the_pictures(i) = LoadPicture(file_path & "picture" & Format$(i) & ".jpg")
    Next i
    End Sub

    Private Sub btnGenerate_Click()
    Randomize
    Do
    RandomNumber = (Rnd * 8)
    PictureDisplay.Picture = the_pictures(RandomNumber)
    Loop Until RandomNumber <> 0 And RandomNumber < 9
    End Sub

    Private Sub Command5_Click()
    Unload Me
    End Sub
    [/code]
  • DaiMitnickDaiMitnick Posts: 77Member
    Strange that loop function should make it keep going until it gets one in range, unless it's somhow getting a negative number or perhaps I've given you the wrong syntax, been a while since using vb6, well anyway probably better to concentrate on the rnd statement, try:

    [red] RandomNumber = CInt(Int((8 * Rnd()) + 1)) [/red]

    Let me know how it goes. Dai
    ------------------------------------------
    Do or do not, there is no try. |
    ------------------------------------------
  • DaiMitnickDaiMitnick Posts: 77Member
    Also, just realised, you may want to try to put empty brackets after Randomize to make it Randomize(), not sure if it helps but it can't hurt either. Dai
    ------------------------------------------
    Do or do not, there is no try. |
    ------------------------------------------
  • SmikeySmikey Posts: 8Member
    I have found the solution with the help of a fellow classmate.

    The problem was in the random number generator i was setting the picture to the randomnumber BEFORE i had carried out the checks on the number value.

    Was a simple matter of swapping those two lines of code over :D

    Silly error.

    Thanks again for the input though
Sign In or Register to comment.