Random Number Generator

is it possible to call a rand() to generate number in ascending order or descending order without sorting it externally? i mean the generated number itself already in ascending or descending order?
You can get a random number and keep it if that was greater than the previous one, otherwise get a different random number
meaning comparison between array of numbers must be done. is it possible to have 1 call then the numbers are already in ascending order? i mean like doing some coding.
closed account (z05DSL3A)
Can you give any more details about what you are trying to achieve?

How many numbers are you talking about what sort of range do you want the numbers to cover...
You don't need to compare with all the number you already have, just the last one

example:
random number = 5
keep 5 as it's the 1st number you get
random number = 8
8 > 5 so keep 8
random number 7
7 < 8 so get another number
random number = 10
10 > 8 so keep 10 (10 > 8 > 5 for sure so you don't need to check it)
Last edited on
closed account (z05DSL3A)
One problem with that, if you hit RAND_MAX you have nowhere to go.

and the higher your number gets the long it would take for each successive call to produce a higher rand.
Last edited on
closed account (z05DSL3A)
soonph87, a priority_queue may help you. Push however many random number you require into it, then just read them of the top and pop them out.
Last edited on
Surely there is a way of generating a random number like this, but im not familiar with random numbers so maybe not.

number = random number
boundary = number
cout number
number = random number + boundary
boundary = number
cout number
etc
Last edited on
closed account (z05DSL3A)
You can, but what happens when the chosen data type can't hold random number + boundary?
what i mean is let's say i wanna generate 100 data within the range 0-200. then, i hope the RNG will generate the data in ascending order or descending order without the need of sorting coding to sort the data.

Grey Wolf, what is priority_queue?

besides, i hope to save the generated data in an array.

p/s: actually i am doing an assignment to sort data in best,average and worst cases. when i generate the data, i need to sort it first before sorting it again for best case and worst case. it seems not very practical.
Last edited on
Get a small number from rand()...then add a small number from rand() to it...then add a small number from rand() to it...
closed account (z05DSL3A)
Priority queues[1] are a type of container adaptors, specifically designed such that its first element is always the greatest of the elements it contains.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <queue>
#include <ctime>

using namespace std;
int main() 
{
    srand((unsigned)time(0));

    const int arraySize = 20;
    const int upperBounds = 200;

    int orderedRandArray[arraySize] = {0};
    
    //
    // Generate and sort the random numbers
    priority_queue<int> orderedRand;
    for (int i = 0; i < arraySize; ++i)
    {
        orderedRand.push( rand() % upperBounds);
    }
    
    //
    // Get the numbers out of the queue and store them.
    for(int i =0; i < arraySize; ++i)
    {
        orderedRandArray[i] = orderedRand.top();
        orderedRand.pop();
        //
        // Show number on screen.
        cout << orderedRandArray[i] << endl;
    }

    return 0;

}


EDIT:
I've just noticed your PS, It sounds like you should be writing the sort code yourself and comparaing different sorting algorithms.[2]

[1] http://www.cplusplus.com/reference/stl/priority_queue/
[2] http://en.wikipedia.org/wiki/Sorting_algorithm
Last edited on
You could also do something like this:

#include <vector>
#include <algorithm>
#include <iostream>

using std::vector;
using std::sort;
using std::cout;
using std::cin;
using std::endl;

unsigned seed;
cout << "Enter a positive integer seed:" << endl;
cin >> seed;
srand(seed);

const unsigned N = 10000; // or whatever
vector<int> v
v.reserve(N);
for(unsigned i = 0; i < N; ++I)
v.push_back( rand(); )

sort(v);
If you're gonna go to that extent, just use a set, which maintains the elements in sorted order automatically.
thanks to all of you!
Topic archived. No new replies allowed.