Re: Generating a random number

Discussion in 'C++' started by Marco de Boer, Jul 1, 2003.

  1. "J. Campbell" <> wrote in message
    news:...
    > (BEECHNUT88) wrote in message

    news:<>...
    > > Hey,
    > > I am having a problem with the program that I an trying to create.

    Im
    > > trying to create a game where the computer generates a random number and

    then a
    > > person has ten guesses to get the number. The number can only be four

    digits,
    > > there cant be a duplicate any of the numbers in the random number. And

    it has
    > > to be between 1023 and 9876. I have no idea how to do this. Can

    someone
    > > pleasseee help me with this. My e-mail address is .

    Thank
    > > you very much.
    > >
    > > Jimmy

    >
    > Jimmy...I don't think the others who responded to your post understood
    > your problem. You say no duplicates...I think you mean that you want


    See my thread (from 'Marco de Boer'): WORKING !
    Unique numbers: [1-9][0-9][0-9][0-9]
    See at the end the example run !

    > each digit of the number to be unique. If this is the case, then I
    > think the easiest way to generate the 4-digit number will be to
    > generate it 1-digit at a time. Since it appears that you want to
    > exclude the possibility of having a zero in the thousands place, I
    > would do something like this. Sorry this is in BASIC
    > (QuickBasic)...I'm just learning C++ and don't know how to find/use
    > functions in the standard library yet...I started to write in C++
    > pseudo code, but didn't want to screw it up since I don't know how to
    > properly call rand(). If you are not used to BASIC, please note:
    > BASIC array(c) == C++ array[c].
    > RND produces a random number between 0 and 1.
    > basic uses "=" for both assignment and as the boolean "is equal"
    >
    >
    > [code follows]
    > DEFINT A-Z 'default type = 2-byte integer
    > CONST false = 0
    > CONST true = NOT false
    >
    > RANDOMIZE TIMER
    > CLS
    > PRINT "This program prints a 4-digit random number with no repeting
    > digits."
    > PRINT "Well...the number isn't really random, since it is a subset"
    > PRINT "of all the available 4-digit numbers."
    >
    > DO
    > REDIM a(4) AS INTEGER '4-element array
    > FOR i = 0 TO 4 STEP 1
    >
    > IF i = 0 THEN
    > a(i) = 1 + (RND * 8) 'special case for first digit...make
    > sure not 0
    > ELSE
    > nodup = false
    > DO 'loop until a unique digit
    > has been selected
    > a(i) = RND * 9 'use 9, not 10 bec
    > (0,1,2...8,9) has 10 elements
    > FOR t = 0 TO i - 1 STEP 1
    >
    > IF a(i) <> a(t) THEN
    > nodup = true 'Here "=" == "="
    > ELSE 'different from c++
    > nodup = false
    > EXIT FOR
    > END IF
    >
    > NEXT t
    > LOOP UNTIL nodup = true 'here "=" == "==" ;-)
    > END IF
    > NEXT i
    >
    > 'turn array into integer
    > mynum = 0
    > FOR i = 0 TO 3 STEP 1
    > mynum = mynum + (1000 / (10 ^ i)) * a(i) 'this is to set the
    > exponent properly
    > 'PRINT mynum; 'if confused, use
    > this line
    > NEXT i
    > PRINT mynum
    > PRINT "make another number?(y/n)"
    > DO: q$ = INKEY$: LOOP UNTIL q$ <> ""
    > LOOP WHILE UCASE$(q$) <> "N"
    > END
    > [end code]
     
    Marco de Boer, Jul 1, 2003
    #1
    1. Advertising

  2. Marco de Boer

    J. Campbell Guest

    "Marco de Boer" <> wrote in message news:<bds98q$r35$1.nb.home.nl>...
    > "J. Campbell" <> wrote in message
    > news:...
    > > (BEECHNUT88) wrote in message

    > news:<>...
    > > > Hey,
    > > > I am having a problem with the program that I an trying to create.

    > Im
    > > > trying to create a game where the computer generates a random number and

    > then a
    > > > person has ten guesses to get the number. The number can only be four

    > digits,
    > > > there cant be a duplicate any of the numbers in the random number. And

    > it has
    > > > to be between 1023 and 9876. I have no idea how to do this. Can

    > someone
    > > > pleasseee help me with this. My e-mail address is .

    > Thank
    > > > you very much.
    > > >
    > > > Jimmy

    > >
    > > Jimmy...I don't think the others who responded to your post understood
    > > your problem. You say no duplicates...I think you mean that you want

    >
    > See my thread (from 'Marco de Boer'): WORKING !
    > Unique numbers: [1-9][0-9][0-9][0-9]
    > See at the end the example run !


    Marco, I was using Google...which has a 3-9 hour delay. Your post
    wasn't up yet when I posted.

    Cheers
     
    J. Campbell, Jul 2, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Leon

    Random Number Generating

    Leon, Oct 18, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    471
    Kevin Spencer
    Oct 18, 2004
  2. Wally
    Replies:
    1
    Views:
    2,811
    pvdg42
    Mar 20, 2006
  3. John Tsiombikas (Nuclear / the Lab)

    Re: Generating a random number

    John Tsiombikas (Nuclear / the Lab), Jul 1, 2003, in forum: C++
    Replies:
    1
    Views:
    370
    David White
    Jul 1, 2003
  4. globalrev
    Replies:
    4
    Views:
    786
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,241
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page