### Random number problem

Hi, I'm needing to generate a list of 10,000 different numbers between -1 and 1. I then have to arrange them in ascending order. Basically I have one array(randomnum[]) filled with the random numbers and I am filling another one(position[]) with the numbers in ascending order. I'm not entirely sure if how I'm generating the numbers is correct, but I feel like everything else is correct. Any help would be appreciated.

 ``12345678910111213141516171819202122232425262728293031`` ``````#include #include #include #include using namespace std; void random(int startpoint, int endpoint); ... ... ...//main function, calls function random(-1, 2) ... ... void random(int startpoint, int range) { double randomnum[10000], position[10000]; int i, j; srand((unsigned)time(0)); for (i=0;i<10000;i++){ position[i] = 1; for (j=0;j<10000;j++){ randomnum[j] = startpoint + (range * rand()) / (RAND_MAX + 1); //here's where I believe my problem to be if ((i == 0) && (randomnum[j] < position[i])) position[i] = random[j]; if ((randomnum[j] <= position[i])&&(randomnum[j] > position[i-1])&&(i > 0)) position[i] = randomnum[j]; } cout << position[i]; } }``````
Have you been able to compile this?
Yes, right now it's giving me a -1, 0, and then a whole bunch of 1s(probably 9998).
Those are awful big arrays to allocate on the stack. I don't think you really need the position array unless you need to preserve the original order for some reason.

This would be a bit simpler with C++11.

 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````#include #include #include #include #include std::vector generateRandom(double min, double max, unsigned num) { std::mt19937 engine((std::random_device()())) ; std::uniform_real_distribution rng(min, max) ; std::vector result(num) ; std::generate(result.begin(), result.end(), [&](){return rng(engine);}) ; return std::move(result) ; } void display(const std::vector& v ) { std::cout << "{\n"; for ( auto element : v ) std::cout << '\t' << std::setw(11) << element << '\n' ; std::cout << "}\n" ; } int main() { unsigned nRandoms = 10 ; // or 10000. // randoms is an unsorted vector of random values. auto randoms = generateRandom(-1, 1, nRandoms) ; display(randoms) ; auto sortedRandoms = randoms ; std::sort(sortedRandoms.begin(), sortedRandoms.end()) ; // sorted Randoms is a sorted vector of random values. display(sortedRandoms) ; }``````

Sample output:
 ```{ -0.129061 -0.326383 -0.275018 0.43802 0.861415 -0.602108 -0.945427 0.450466 -0.261912 -0.321775 } { -0.945427 -0.602108 -0.326383 -0.321775 -0.275018 -0.261912 -0.129061 0.43802 0.450466 0.861415 } ```

Last edited on
I do need to keep the originally generated list (generatednum[]). I've checked that list and it is only producing -1s and 0s (0000-100-1-10-1 etc.). Definitely something wrong with my rand()
You might try:

`randomnum[j] = startpoint + rand()/(RAND_MAX/static_cast<double>(range)); `
for line 23. It's integer division as it stands now.
Thank you that worked. Now however, my sorted list is wrong, haha. I believe what's happening is that I'm generating a new list for every iteration of i, so it's using different numbers each time. Could I just define

`randomnum[10000]=startpoint + rand()/(RAND_MAX/static_cast<double>(range)); `

outside of my for loops?

EDIT: Nope
Last edited on
I have it figured out, thank you everyone
Topic archived. No new replies allowed.