Boost logo

Boost Users :

Subject: Re: [Boost-users] Random Numbers using Boost.
From: Marshall Clow (marshall_at_[hidden])
Date: 2009-01-12 23:56:11


At 7:21 PM -0600 1/12/09, Atul Kulkarni wrote:
>Hi All,
>
>I am using boost::random for generating random numbers I am using
>the default quick start code given up on the web page. I want to
>generate random numbers between range 1 - n so that they do not
>repeat themselves for at least n numbers.
>
>1. Is it possible using boost::random?
>2. If it is possible what do I need to do except the below code.
>
>==
> boost::mt19937 rng1;
> boost::uniform_int<> six1(1,30);
> boost::variate_generator<boost::mt19937&,
>boost::uniform_int<> > die1(rng1, six1);
> for(int i=0; i < 29; i++)
> {
> cout<<die1()<<endl;
> }
>==

This will not satisfy your preconditions, except by accident.
A random number generator will, at random times, output the same
number twice in a series.

If you want to choose numbers w/o repeating, that's more akin to
shuffling the cards in a deck - and there are lots of online
resources on how to do that (alternately, you can look in Knuth).
See <http://en.wikipedia.org/wiki/Shuffling#Shuffling_algorithms> for one.

-- 
-- Marshall
Marshall Clow     Idio Software   <mailto:marshall_at_[hidden]>
It is by caffeine alone I set my mind in motion.
It is by the beans of Java that thoughts acquire speed,
the hands acquire shaking, the shaking becomes a warning.
It is by caffeine alone I set my mind in motion.

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net