Issue about random numbers

I'm making a game and i have 20 events.I want to them to occur 5 timses ( 5 events ) and what they would all be random.For events I'm using if ( if random number is x ) , and a function after that random number.

I can make them appear , but sometimes couple of them are the same.


I appreaciate any help towards this issue!



Can You Give Us The Code You Have So Far So We Can Help You?

That code is written in my own language ( i' m not an english speaker )

Scenario1_JK_after_preparations_random_event_1 = rand()%20 ;

[FUNCTION1]

Scenario1_JK_after_preparations_random_event_2 = rand()%20 ;

[FUNCTION2]

Scenario1_JK_after_preparations_random_event_3 = rand()%20 ;

[FUNCTON3]

Scenario1_JK_after_preparations_random_event_4 = rand()%20 ;

[FUNCTION4]

Scenario1_JK_after_preparations_random_event_5 = rand()%20 ;

[FUNCTION5]


[FUNCTION 1 - events from 0 - 19 ( total 20 ) and if ( Scenario1_JK_after_preparations_random_event_1 = 1 , 2 and etc )


[FUNCTION 2 - events from 0 - 19 ( total 20 ) and if ( Scenario1_JK_after_preparations_random_event_2 = 1 , 2 and etc ]


[FUNCTION 3 - events from 0 - 19 ( total 20 ) and if ( Scenario1_JK_after_preparations_random_event_3 = 1 , 2 and etc ]


[FUNCTION 4 - events from 0 - 19 ( total 20 ) and if ( Scenario1_JK_after_preparations_random_event_4 = 1 , 2 and etc ]

[FUNCTION 5 - events from 0 - 19 ( total 20 ) and if ( Scenario1_JK_after_preparations_random_event_5 = 1 , 2 and etc ]

I would post all code , but it's too large , and written in my own langauge!


Anyone could help?
Basically you want to chose 5 events out of 20 with no duplicates, yes?

You need to use a do-while loop which calls rand() until you get a number you haven't gotten before. So you will need to store the number you've already chosen in either a C-style array (or a standard container, if you're familiar with them) so you can check whether or not a random number has been used.

Andy
@andywestken

Thanks for your response , but i m not familiar with do-while loop,only with while loop .
Well...

Searching this site for "do while" I get, as the first hit,

Statements and flow control
http://www.cplusplus.com/doc/tutorial/control/

which has a section "The do-while loop"

Andy
@andywestken

Thanks again, but from that i couldnt understand it really.
One guy told me what if i'm not capable of thinking this myself,i shouldn't be programming at all.Hope i find the answer :D
Randomly shuffle the 20 events, and pick the first five events in the shuffled sequence.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <random>

int main()
{
    const std::size_t NEVENTS = 20 ;
    const std::size_t NRANDOMLY_CHOSEN = 5 ;

    // create a sequence of NEVENTS events
    std::vector<std::string> events ;
    for( std::size_t i  = 0 ; i < NEVENTS ; ++i ) events.push_back( "event #" + std::to_string(i) ) ;

    // shuffle the sequence randomly http://en.cppreference.com/w/cpp/algorithm/random_shuffle
    std::shuffle( std::begin(events), std::end(events), std::mt19937( std::random_device()() ) ) ;

    // select the first NRANDOMLY_CHOSEN events in the sequence
    for( std::size_t i = 0 ; i < NRANDOMLY_CHOSEN ; ++i ) std::cout << i+1 << ". " << events[i] << '\n' ;
}

http://coliru.stacked-crooked.com/a/81eee574b6ca18b7
@JL.Borges

Thanks , but i have no idea what u have written here.Wish i understand it.
This may be easier to understand:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstdio>
#include <ctime>
#include <algorithm>

int main()
{
    const std::size_t NEVENTS = 20 ;
    const std::size_t NRANDOMLY_CHOSEN = 5 ;

    // create a sequence of NEVENTS events 0, 1, 2 ... NEVENTS-1
    int events[NEVENTS] ;
    for( std::size_t i  = 0 ; i < NEVENTS ; ++i ) events[i] = i ;

    // shuffle the sequence randomly http://en.cppreference.com/w/cpp/algorithm/random_shuffle
    std::srand( std::time(0) ) ;
    std::random_shuffle( events, events+NEVENTS ) ;

    std::cout << "the randomly chosen event numbers are: " ;
    // select the first NRANDOMLY_CHOSEN events in the sequence
    for( std::size_t i = 0 ; i < NRANDOMLY_CHOSEN ; ++i ) std::cout << events[i] << ' ' ;
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/c7052eaf1ee01145
@JLBorges

Is it possible to make this without:

#include <cstdio>
#include <algorithm>

size_t


and etc...
?
How can u make as easy as possible without vectors and stuff ... i mean almost with basic stuff , loops , rand() ?
> Is it possible to make this without:
> #include <cstdio>

Yes. #include <cstdio> was a typo; should have been #include <cstdlib>

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
#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
    using namespace std ;

    const int NEVENTS = 20 ;
    const int NRANDOMLY_CHOSEN = 5 ;

    // create a sequence of NEVENTS events 0, 1, 2 ... NEVENTS-1
    int events[NEVENTS] ;
    for( int i  = 0 ; i < NEVENTS ; ++i ) events[i] = i ;

    srand( time(0) ) ;

    cout << "the randomly chosen event numbers are: " ;
    for( int i = 0 ; i < NRANDOMLY_CHOSEN ; ++i )
    {
        // choose a random position from the remaining (NEVENTS-i) events starting at position i
        // (the events at positions 0, 1, ... (i-1) have been chosen earlier)
        const int chosen_pos = i + rand() % (NEVENTS-i) ;

        // swap the randomly chosen event with the event at position i
        int temp = events[i] ;
        events[i] = events[chosen_pos] ;
        events[chosen_pos] = temp ;

        cout << events[i] << ' ' ; // print out the chosen event
    }
    cout << '\n' ;
}
@JLBorges

I mean can u do it with basic stuff
// thats all the libraries which you can use
#include <iostream>
#include <ctime>
@JLBorges

Havy any ideas?
THese two libraries do not provide any random number generating capabilities.
So if you want to use only those two libraries, you need to either forget about random numbers, or write random number generator yourself.
@MiiniPaa

Thanks :)
@JLBorges

Thanks you too!
Topic archived. No new replies allowed.