AboutAlex Barry Expertise I have been a qbasic programmer since 2000, creating games, minor libraries and various small programs. I have experience using interrupts, graphics, file input/output, the mouse cursor, and using libraries. I have also learned FreeBASIC, c/c++, python, php and html.
I do not claim to be an absolute authority in any language, but I don't mind looking things up and learning with you.
Experience Hobby programming since 2000
I no longer belong to any community programming groups, but do have knowledge of *basic dialects and C/C++
Question hey i was wondering if you could help me with my assignments. In one of them I have to print 6 random numbers in the range of 1 to 40. This is my code, which prints the numbers but prints the same ones time after time:
Cls
Dim randnum As Integer
For randnum = 1 To 6
Print Int(Rnd * 10)
Next randnum
As you can see Im not an expert in qbasic or in programming, but Im real keen on learning as I want to take it as a career
Answer Hi, Jorge,
I'm glad to hear you want to be a programmer. I'll say as much that QBasic doesn't have a huge future in it, but I do encourage people to learn it - it's fairly easy to pick up, and a lot of the concepts are transferable to other languages.
Also, thanks for providing some code, I only do homework questions when people show me their work, so I appreciate that.
To make your code work, and do new numbers every time, do this at the top of your code:
RANDOMIZE TIMER
This sets the random number seed to a new number everytime (in this case, the seed is the value of TIMER, a built-in qbasic function)
Now, right now, your code will produce numbers between 0 and 9
To change the numbers to 1 and 40, do this:
Print INT( Rnd * 40 ) + 1
Rnd produces a number between 0 and 0.999999~, and with integers, it ignores the decimal place. So, multiply it by the maximum number, and it becomes between 0 and (in this case) 39, then add 1, and you have between 1 and 40 :)