Random number generator

Sep 16, 2019 at 10:18pm
I am working on a code that generates 35 random numbers between 0 and 199. Here is what I have so far. However, I would like to now have it sort by low to high values. Can someone provide guidance?


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
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
int i;
int array[35];
int odd;
int Max;
int counter = 0;


int main()

{	
	cout << "35 random elements are: ";
	cout << endl;

	srand ( time(0) );

	for (int j = 0; j < 35; j++)
	{

		i = rand() % 200;
		 

		 if (i != i-1)
			array[j]=i;

		 else
		 {
			i = rand() % 200;
		 array[j] = i;
		 }
	}
	
	for (int k = 0; k < 35 ; k++)
	{
		cout << array[k] << "\n ";

	}
	cout << endl;
	
	
	return 0;
}
Last edited on Sep 16, 2019 at 10:18pm
Sep 16, 2019 at 10:30pm
std::sort(array, array + 35); should work.
Sep 16, 2019 at 10:35pm
Hello CodingIsHard17,

"std::sort(...)". Include "<algorithm>" header file.

Andy
Sep 17, 2019 at 8:03pm
Hello,

How can I print the random numbers separately from the sorted version? Also, how do I sort using quicksort?
Sep 17, 2019 at 8:27pm
you can print, then sort, then print, is one way.
you can copy and sort and have 2 copies, is a wasteful way, but fine for homework.
quicksort ... if you want it, you have to write it or get one of the bajillion student homework versions online. C++ does not provide an algorithm choice for sorting in the libraries. C's sort MAY still be quicksort or may be if you use an older compiler. Odds are they have also updated it to intro for up to date tools.
Last edited on Sep 17, 2019 at 8:29pm
Sep 17, 2019 at 9:22pm
Create a boolean array of size 200, all initialised to false.

Choose 35 distinct elements to turn true.

Take the true elements from indices 0 to 199 to "sort" ascending.
Take the true elements from indices 199 to 0, step -1, to "sort" descending.



Alternatively, generate random numbers and stick them in a set<int> until the size of that set is 35.
Then just iterate through the set; they'll be in ascending order.
Last edited on Sep 17, 2019 at 9:28pm
Sep 18, 2019 at 3:12am
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
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <numeric>
#include <iterator>
#include <random>
#include <algorithm>

int main()
{
    int rand_array[35] {} ;

    // **** generate 35 unique random numbers between 0 and 199
    // sorted in ascending order, and place them into rand_array ****

    {
        // 1. create an array holding values in [0 .. 199]
        int temp[200] {} ;
        // https://en.cppreference.com/w/cpp/algorithm/iota
        // https://en.cppreference.com/w/cpp/iterator/begin
        std::iota( std::begin(temp), std::end(temp), 0 ) ;

        // 2. shuffle the array randomly
        // https://en.cppreference.com/w/cpp/algorithm/random_shuffle
        // https://en.cppreference.com/w/cpp/numeric/random
        std::shuffle( std::begin(temp), std::end(temp),
                      std::mt19937( std::random_device{}() ) ) ;

        // 3. copy the first 35 elements into rand_array
        // http://www.stroustrup.com/C++11FAQ.html#static_assert
        // https://en.cppreference.com/w/cpp/iterator/size
        static_assert( std::size(rand_array) <= std::size(temp), "invalid array size" ) ; // sanity check
        // https://en.cppreference.com/w/cpp/algorithm/copy
        std::copy( std::begin(temp), std::begin(temp)+std::size(rand_array),
                   std::begin(rand_array) ) ;

    }
    // 4. print the array of random numbers
    // http://www.stroustrup.com/C++11FAQ.html#for
    for( int r : rand_array ) std::cout << r << ' ' ;
    std::cout << '\n' ;

    // 5. sort the array of random numbers in ascending order
    // https://en.cppreference.com/w/cpp/algorithm/sort
    std::sort( std::begin(rand_array), std::end(rand_array) ) ;

    // 6. print the array of random numbers in ascending order
    for( int r : rand_array ) std::cout << r << ' ' ;
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/aaa32a2493d40d91
https://rextester.com/JLJBH90694
Sep 18, 2019 at 11:04am
If you are prepared to allow duplicates (and my previous post assumed you didn't):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <algorithm>
using namespace std;

int main()
{
   const int N = 35;
   int a[N];
   srand( time( 0 ) );
   generate( a, a + N, [](){ return rand() % 200; } );
   sort( a, a + N );
   for ( auto e : a ) cout << e << ' ';
}



If you don't allow duplicates, then:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <set>
using namespace std;

int main()
{
   const int N = 35;
   set<int> a;
   srand( time( 0 ) );
   while ( a.size() < N ) a.insert( rand() % 200 );
   for ( auto e : a ) cout << e << ' ';
}
Last edited on Sep 18, 2019 at 11:09am
Sep 18, 2019 at 11:48am
The right way of using an uniform integer distribution is to use std::uniform_int_distribution with a standard C++ random generator like Mersenne twister

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
#include <iostream>
#include <algorithm>
#include <vector>
#include <random>

int main() {
    std::vector<int> rand_array;
    std::mt19937 gen(std::random_device{}());
    std::uniform_int_distribution<> dis(0, 199);

    for(int i=0; i < 35; ++i) {
        rand_array.push_back(dis(gen));
    }

    // print
    for(int r : rand_array) {
        std::cout << r << " ";
    }
    std::cout << "\n";

    // sort
    std::sort(std::begin(rand_array), std::end(rand_array));

    // print again
    for(int r : rand_array) {
        std::cout << r << " ";
    }
    std::cout << "\n";
}
Last edited on Sep 18, 2019 at 11:49am
Sep 18, 2019 at 4:09pm
Returning to your original code, you seem to have a bug: it allows duplicates:
1
2
if (i != i-1)    // always true
	array[j]=i;

If you fixed this to find duplicate entries, it would still have a bug:
1
2
3
4
5
 else
 {
	i = rand() % 200;   // what if this is also a duplicate?
	array[j] = i;
 }


Your basic algorithm needs to be:
1
2
3
4
5
6
do 35 times {
    do {
        pick a random number
    } while (the number is already in the array)
    insert the number in the array
}


Alternatively you could use lastchance's method (let std::set() take care of duplicates) or JL Borges's (create an array of 200 number and then pick 35 random values from the array).
Sep 19, 2019 at 11:27pm
Hey. Thank you for the help and guidance everyone. I don't mind duplicates. I'm just trying to figure out how to use quicksort to sort my random numbers separately from the generated numbers.
Sep 20, 2019 at 5:56pm
sort my random numbers separately from the generated numbers.

Create your list of 35 randomly generated numbers, using the container of your choice. C-style array, std:vector, whatever.

Create a copy of that list and sort the copy. Now you have the original list and a sorted copy.

I personally would use either std::vector or std::array.
Sep 20, 2019 at 6:02pm
If you are looking specifically for how to implement quicksort yourself, just search for it or watch a youtube video that explains it. There are probably hundreds of sites that will show an implementation of quicksort. (Note that the C library's standard function is actually called qsort, which is also in C++, but if you're using C++ you should be using std::sort.)
Last edited on Sep 20, 2019 at 6:03pm
Sep 24, 2019 at 1:23am
Thank you. I'll try to figure something out then.
Sep 24, 2019 at 2:01am
Topic archived. No new replies allowed.