Velocity Reviews > C++ > Re: Generating a random number

Re: Generating a random number

Marco de Boer
Guest
Posts: n/a

 07-01-2003
"J. Campbell" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> http://www.velocityreviews.com/forums/(E-Mail Removed) (BEECHNUT8 wrote in message

news:<(E-Mail Removed)>...
> > 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 (E-Mail Removed).

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 * '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]

J. Campbell
Guest
Posts: n/a

 07-01-2003
"Marco de Boer" <(E-Mail Removed)> wrote in message news:<bds98q\$r35\$(E-Mail Removed)1.nb.home.nl>...
> "J. Campbell" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> > (E-Mail Removed) (BEECHNUT8 wrote in message

> news:<(E-Mail Removed)>...
> > > 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 (E-Mail Removed).

> 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