[prev in list] [next in list] [prev in thread] [next in thread] 

List:       nsbasic-palm
Subject:    Re: [nsbasic-palm] Random numbers between two ranges
From:       Douglas Handy <dhandy1 () bellsouth ! net>
Date:       2006-09-30 11:53:56
Message-ID: 6rlsh2dn45fkij68q426c63gn4aoabuh8s () 4ax ! com
[Download RAW message or body]

Aftab,

>Say, I want random numbers between 2 to 5 and with each click of a
>button, the random numbers will be displayed without duplication.
>
>e.g. 3,2,4,5 (not 2,3,5,5)

Random does not mean the number can't repeat; it means the next number isn't
predictable.  Say you are rolling a dice.  You may get any number from 1 to 6.
However, if you roll it 6 times, you are not guaranteed to not have any
duplicates.

To eliminate duplicates, as others have pointed out you need to do one of two
things:

 1) Keep a list of possible choices (think deck of playing cards).  Use Rand()
multiplied by the number of items remaining in the list to help select the next
choice, then remove that from the list.

or

 2) Keep a list of choices previously displayed.  When you generate a new random
number, first check to see if it is in your list of previously displayed values.
If so, try again until it is not.  Display, and add to your list of previous
values.

The first method is preferable when you have a relatively small potential range
or finite list, such as a deck of playing cards.

The second method is preferable when you are dealing with larger ranges, and are
using a subset of the numbers.  For example, if you wanted a list of 100 random
numbers between 1 and 1,000,000 but could not have duplicates you'd want to use
the second method instead of the first.

Doug



Complete Searchable Archive:
 http://marc.theaimsgroup.com/?l=nsbasic-palm&r=1&w=2

Shortcut URL to this page:
 http://groups.yahoo.com/group/nsbasic-palm  
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/nsbasic-palm/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/nsbasic-palm/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:nsbasic-palm-digest@yahoogroups.com 
    mailto:nsbasic-palm-fullfeatured@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
    nsbasic-palm-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic