Hi everyone.

Can anyone please guide me on how can I generate a random number from a group of prset numbers.


For example:-

There is a group of preset numbers:-
1,5,18,100,127,500,626,929.

I want to generate a random number from this group....is there anyway in which we could do this?

Dani AI

Generated

Short answer: store your preset values in a container and pick a random index. 's suggestion is exactly the right idea — below are concise, safe C++ ways to do it and a couple of pitfalls to avoid (as hinted by pointing to related threads).

A modern, robust approach (C++11+) — use <random> and a single generator you reuse. This avoids modulo bias and poor seeding patterns:

#include <iostream>
#include <vector>
#include <random>

int main() {
    std::vector<int> preset = {1, 5, 18, 100, 127, 500, 626, 929};
    if (preset.empty()) return 0;

    std::random_device rd;
    std::mt19937 gen(rd());                     // or fixed seed for reproducible runs
    std::uniform_int_distribution<std::size_t> dist(0, preset.size() - 1);

    int pick = preset[dist(gen)];
    std::cout << pick << '\n';
}

If you need k unique picks (no repeats), shuffle and take the first k, or use std::sample (C++17). Example using shuffle:

#include <algorithm> // for shuffle
// assume 'preset' and 'gen' from above
std::shuffle(preset.begin(), preset.end(), gen);
size_t k = 3;
for (size_t i = 0; i < k && i < preset.size(); ++i) std::cout << preset[i] << ' ';

Troubleshooting notes: check for empty container; use size_t for indices; avoid calling rand()/srand() and rand() % n (modulo bias and weaker RNG). Don’t reseed the generator on every pick — create one mt19937 and reuse it. For weighted selection, use std::discrete_distribution. If you need cryptographic randomness, use an OS crypto API rather than the standard PRNG.

Recommended Answers

All 4 Replies

Do you mean how can you pick up a random number from given sequence of numbers?
follow this thread :http://www.daniweb.com/forums/thread198944.html

Lemme try and explain it in more details.
I provide the program with the following group of numbers:-

1,5,18,100,127,500,626,929.

Now, all I want is that the output should be a random number from one of these numbers:-

1,5,18,100,127,500,626,929.

This is quite simple actually,

Put all of the Numbers into a array,

Then Generate a random number between 0 and {The Number of Numbers in your array } Including 0 too, So then you can use array [randomnumberindex] to get a random number out,

This is quite simple actually,

Put all of the Numbers into a array,

Then Generate a random number between 0 and {The Number of Numbers in your array } Including 0 too, So then you can use array [randomnumberindex] to get a random number out,

Oh...lol...okay...yeah..that was simple. :D

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.