Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,971 questions

51,913 answers

573 users

How to random (shuffle) an array of ints in C++

2 Answers

0 votes
#include <iostream>
#include <algorithm>
#include <ctime>
 
using std::cout;
using std::endl;
 
int main()
{
    int arr[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 
    std::srand(std::time(0));
 
    std::random_shuffle(std::begin(arr), std::end(arr));
 
    for (auto i : arr) {
        cout << i << " ";
    }
 
    cout << endl;
}
 
 
/*
run:
 
6 2 5 9 4 1 8 0 7 3 
 
*/

 



answered Feb 20, 2018 by avibootz
edited May 17, 2024 by avibootz
0 votes
#include <iostream>
#include <algorithm>
#include <ctime>
 
using std::cout;
using std::endl;
 
int main()
{
    int arr[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    int size = sizeof(arr) / sizeof(arr[0]);

    std::srand(std::time(0));
 
    std::random_shuffle(arr, arr + size);
 
    for (auto i : arr) {
        cout << i << " ";
    }
 
    cout << endl;
}
 
 
 
/*
run:
 
7 0 2 9 8 5 3 4 1 6 
 
*/

 



answered May 17, 2024 by avibootz

Related questions

2 answers 194 views
1 answer 158 views
1 answer 163 views
1 answer 143 views
1 answer 137 views
137 views asked Dec 30, 2017 by avibootz
2 answers 146 views
146 views asked Nov 1, 2021 by avibootz
1 answer 162 views
162 views asked Oct 7, 2019 by avibootz
...