Qbasicnews.com

Full Version: Catching repeat numbers
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
i'm trying to procue a list of numbers that are random but not repearting. this is what i have so far and it was all done by me with no help ! Big Grin
Code:
CLS
RANDOMIZE TIMER
FOR i = 1 TO 6
num(i) = INT(RND * 15) + 1
NEXT
f = 2
FOR x = 1 TO 6
FOR y = f TO 6
IF num(x) = num(y) THEN
loca = y
GOTO new
END IF
NEXT
f = f + 1
NEXT

GOTO start

new:
new = INT(RND * 15) + 1
FOR x = 1 TO 6
IF new = num(x) THEN GOTO new
NEXT
PRINT "new num = "; new
SLEEP 1
GOTO start

start:
num(loca) = new
FOR i = 1 TO 6
PRINT num(i)
NEXT

the problem is that it will only catch the first repeat. example 1 3 4 4 3. it will only catch the 3's not the 4's. that's because of IF num(x) = num(y) THEN. once that is true it goes to make the new number. how do i get it to check all the numbers for repeats and store which numbers are repeated??
Code:
CLS
DIM numbers(1 TO 15)
RANDOMIZE TIMER

FOR I = 1 to 15
again:
  randomnum = INT(RND * 15) + 1
  FOR X = I - 1 TO 1 STEP -1
    IF randomnum = numbers(X) THEN GOTO again
  NEXT X
  numbers(I) = randomnum
  PRINT "Number "; I; ":"; numbers(I)
NEXT I
thanks, i understand must of it but the FOR X = I - 1 TO 1 STEP -1 is a little confusing. would you please explain it. this wat is much better than what i was doing.
OK. In the code, we start a loop to get the right amount of numbers (FOR I = 1 TO 15). We generate a random number. Then we check it against all the random numbers we had previously with FOR X = I - 1 TO 1 STEP -1.

STEP -1 simply means that we're counting backwards. So instead of FOR I = 1 TO 10 going 1,2,3,4,5... FOR I = 10 TO 1 STEP -1 means 10,9,8,7...

We check from I-1, because that's the index of the last number we generated, and check to 1 because that's the first index.

ps: for lotsa random numbers, use a DEFINT at the top.
Quote:*aims nukes at Diroga...*

?
Quote:i'm trying to procue a list of numbers that are random but not repearting.....
the problem is that it will only catch the first repeat. example 1 3 4 4 3. it will only catch the 3's not the 4's. that's because of IF num(x) = num(y) THEN. once that is true it goes to make the new number. how do i get it to check all the numbers for repeats and store which numbers are repeated??

Diroga, someone else might have already suggested this. Rather than generate a random number then test if it has been picked already, why not do it the way it's done in real life...think of a deck of cards, or the lotto-ball-tumbler. Put all the numbers in a hat, mix them up, then draw them out, one at a time.

1) make an array and stock it with your numbers.
2) mix up the array.
3) extract the values from your hat
Code:
DEFINT A-Z
RANDOMIZE TIMER
CLS
INPUT "How many cards in YOUR deck ;-)"; arraysize

DIM array(arraysize) AS INTEGER

FOR i = 0 TO arraysize             'create your hat & stock it with numbers
   array(i) = i
NEXT i

DO
  shuffledenough = 0
  DO
    DO
      x = INT(RND * arraysize)
      y = INT(RND * arraysize)
    LOOP WHILE (x = y)      'loop if x and y are the same number...oops...try again...no biggie.
  
    SWAP array(x), array(y)
    shuffledenough = shuffledenough + 1   ' count each time a swap is made
  LOOP UNTIL (shuffledenough > 100)       'exit loop after 100 swaps
  FOR i = 0 TO arraysize
    PRINT array(i);                       'here are your non-repeating "random" values
  NEXT i

  PRINT
  INPUT "shuffle again(y/n)"; p$
LOOP UNTIL UCASE$(p$) = "N"
*nukes Diroga*

Ahhhh...... relief......
Wow, thanks. i'm really new to progrmaing. i have played with basic for four years now and have never tried devled into it. i'm a capible thinker, but i general y see things one way and try to work on them in only one light. yeah so i understand what your saying. i dont get two parts

LOOP UNTIL (shuffledenough > 100)

i know that is loop untill the value of shuffledenough is greater thatn 100. but why?

LOOP WHILE (x = y)
SWAP array(x), array(y)

i dont full get what swap does. and how does array(x) and array(y) get a value in for the numbers in the array?

thanks so much for the help and pacientces.
SWAP exchanges the values of two variables. For example,
In another thread of yours, I used
Code:
FOR j= 1 TO 16
  Temp = RndmNmbr(j)
  r = INT(RND * 16 +1)
  RndmNmbr(j) = RndmNmbr(r)
  RndmNmbr(r) = Temp
NEXT
to shuffle the numbers in an array by trading places of pairs of elements in the array.

Aga then did the same thing using SWAP in three lines instead of my six:
Code:
FOR i% = 1 TO 16
SWAP names.index%(INT(RND*16)+1), names.index%(i%)
NEXT i%
Pages: 1 2