Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Re: Generating a random number

 
 
Stewart Gordon
Guest
Posts: n/a
 
      07-01-2003
J. Campbell wrote:

<snip>
> 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."


By "available" do you mean "from the whole set of 4-digit numbers with
no repeating digits"? If so, I can't see any that it'll miss. Which
numbers are they?

> 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


Does the OP want a uniform distriution? Well, this doesn't even attempt
to produce one.

Should use

a(i) = INT(1 + RND*9)

and similarly with RND*10 for subsequent digits.

> 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 "=" == "="


Why not initialise nodup = true just before the for loop rather than
setting it 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

<snip>

I guess slightly better would be something like

mynum = mynum + 10^(3-i) * a(i)

or even

FOR i = 3 TO 0 STEP -1
mynum = 10 * mynum + a(i)
NEXT i

Stewart.

--
My e-mail is valid but not my primary mailbox. Please keep replies on
on the 'group where everyone may benefit.

 
Reply With Quote
 
 
 
 
J. Campbell
Guest
Posts: n/a
 
      07-01-2003
Stewart Gordon <(E-Mail Removed)> wrote in message news:<bdscuq$484$(E-Mail Removed)>...
> J. Campbell wrote:
>
> <snip>
> > 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."

>
> By "available" do you mean "from the whole set of 4-digit numbers with
> no repeating digits"? If so, I can't see any that it'll miss. Which
> numbers are they?
>

Hi Stuart. I was just trying to get the original poster headed in the
right direction. That was sloppy 5-min code. The point here was that
a randomly selected 4-digit number with no repeting digits is
different than a 4-digit random number. Thanks for the correction re
rnd...my code underrepresented the min and max value.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM
Random number generator, generating 10 different numbers. Need Help. Wally ASP .Net 1 03-20-2006 12:19 AM
Random Number Generating Leon ASP .Net 6 10-18-2004 05:56 PM
Re: Generating a random number John Tsiombikas (Nuclear / the Lab) C++ 1 07-01-2003 10:38 AM



Advertisments