Hi,
can somebody tell me how i can generate a random number sequence,within a specific range?

use the mod operator % to get a value from 0 to some upper-limit. For example to get a random number between 0 and 100 int num = rand() % 100; Much more information about it here.

But i would'nt get a sequence,what i need is a sequence,ie a the numbers should not be repeated in my sequence,all numbers must be unique.

But i would'nt get a sequence,what i need is a sequence,ie a the numbers should not be repeated in my sequence,all numbers must be unique.

There are some decent threads here on Daniweb regarding this. Type in "random numbers unique", "random numbers no duplicate", without the quotes, and similar, and some decent threads come up. Here's one I commented on:

http://www.daniweb.com/forums/thread109663.html

Post 4 is the relevant post. It's a little confusing since two threads got combined, but that should give you an idea. I suggested in that post (at least I THINK that was me, if memory serves) that the OP set up a boolean array. When a number is randomly generated, check that array. If it's already been picked, select another. When you get a new random number, set that boolean array to having been picked already.

There's that thread and many others. Like i said, type in keywords similar to what I suggested above, and some useful threads will pop up regarding ways to assure non-repetitiveness of random numbers.

> ie a the numbers should not be repeated in my sequence,all numbers must be unique
That's not a sequence, it's a set, and most random number generators don't guarantee uniqueness. You have to force that invariant yourself:

#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <limits>
#include <set>
#include <sstream>
#include <vector>

class random_set {
  std::vector<int> _src;
  std::vector<int>::size_type _index;
public:
  random_set(int lo, int hi, int n = std::numeric_limits<int>::max());
  std::vector<int>::const_iterator begin() const { return _src.begin(); }
  std::vector<int>::const_iterator end() const { return _src.end(); }
};

random_set::random_set(int lo, int hi, int n)
  : _index(0)
{
  std::set<int> temp;

  // Validate the range
  if (lo >= hi) {
    std::ostringstream oss;

    oss << "The range's lower bound matches or exceeds the "
      "upper bound: [lo = " << lo << ", hi = " << hi << "]\n";

    throw std::invalid_argument(oss.str());
  }

  // Insert random numbers until the limit or full range is reached
  while (temp.size() < (unsigned)(hi - lo) && temp.size() < n)
    temp.insert(lo + std::rand() % (hi - lo));

  // Fill the source list and shuffle the numbers
  _src.insert(_src.begin(), temp.begin(), temp.end());
  std::random_shuffle(_src.begin(), _src.end());
}

int main()
{
  try {
    std::srand(std::time(0));
    
    random_set r(-5, 5);

    std::copy(r.begin(), r.end(),
      std::ostream_iterator<int>(std::cout, "\n"));
  } catch (std::exception& ex) {
    std::cerr << ex.what() << '\n';
  }
}

But i would'nt get a sequence,what i need is a sequence,ie a the numbers should not be repeated in my sequence,all numbers must be unique.

Hi kaos,

there isn't a primal emptiness, space on the internet! So ever tried typing in "unique random numbers" at google? Guess how many entries you would have found then. Exactly, about 5,370,000 just within wee 0.21 seconds!

krs,
tesu

Sorry it should be 'set' and not 'sequence' as per Radical Edward's implication

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.