5

I need a totally random number from 0 to 10 or from 0 to 100 as a value "NUM" done in QBasic for a random draw program. I currently have this:

RANDOMIZE TIMER: A = INT((RND * 100)): B = INT((RND * 10)): C = (A + B)
NUM = INT(C - (RND * 10))

This is basically just a pile of random mathematical operations to get a random number from 1 to 100.

The problem is i get the same or very similar numbers quite too often. Is there a more reliable way to do this?

Gala
  • 2,592
  • 3
  • 25
  • 33
  • 4
    The pseudo-random number generator in QBasic is of very low quality and seems to be biased towards certain outputs (based on my experience with it in the 1990s). I'd guess that it only has 16 bits of state. If you need a higher-quality PRNG in QBasic, you may need to write your own. – user3553031 Jul 20 '14 at 21:11
  • So this is as random as it gets? – Gala Jul 20 '14 at 21:29
  • 2
    As long as you're doing `RANDOMIZE TIMER`, it's about as good as QBasic gets. That said, you should be able to do it in one operation as technically your code will give `-10 < NUM < 110`. Anyway, thanks for the trip down nostalgia lane. Anyone else remember [nibbles](https://www.youtube.com/watch?v=UmeKHtei0qo) and [gorillas](https://www.youtube.com/watch?v=UDc3ZEKl-Wc)? – Basic Jul 21 '14 at 07:15

3 Answers3

3

The code that you provide does not at all meet the requirement of a "random number from 0 to 10 or from 0 to 100". You start out setting A to a random number from 0 to 99 and B to a random number from 0 to 9. But the rest of your calculation does not perform an "OR".

How about this:

RANDOMIZE TIMER
A = INT(RND * 101)      // random number from 0 to 100
B = INT(RND * 11)       // random number from 0 to 10
C = INT(RND * 2)        // random number from 0 to 1
IF C = 0 THEN
   NUM = A
ELSE
   NUM = B
END IF

or more simplified:

RANDOMIZE TIMER
NUM = INT(RND * 101)
IF INT(RND * 2) = 0 THEN
   NUM = INT(RND * 11)
END IF
dokaspar
  • 8,186
  • 14
  • 70
  • 98
2

Try this Rand function used as

NUM = Rand(0, 100)
bugmagnet
  • 7,631
  • 8
  • 69
  • 131
  • 1
    A minor point is it also uses QBasic's native `RND`, where I was hoping to see a Mersenne Twister (or, in fact, *anything* else). But +1: confusing the RND for "more randomness" made me smile :) – Jongware Jul 25 '14 at 12:08
  • 2
    @Jongware In my ample spare time (NOT!) I was hoping to try some of the algorithms listed at http://stackoverflow.com/questions/6275593/how-to-write-you-own-random-number-algorithm (particularly the XORShift) and http://en.wikipedia.org/wiki/List_of_pseudorandom_number_generators . The QBASIC implementation thereof should be an interesting (insanitizing?) challenge. – bugmagnet Jul 25 '14 at 13:43
1

For getting a random number from 0 to any number , you can use the following program -

Randomize timer

Rand_num = INT(RND(1)*limit)+1

Where-

Rand_num= the random number

Limit = the last number like if you want from 0 to 100 then limit will be 100
AlexB
  • 7,302
  • 12
  • 56
  • 74