Click here to Skip to main content
15,915,509 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey, I want to know how to get 10 random number but between 1 to 20 and every time that 10 no. Should be different.
Like 2,8,11,14,4,7,9,18,14,20
The number should not repeat
And store the value in array[ ]

What I have tried:

I tried the rand command but it gives me a random number not number between 1 to 20 and I can't store it
Posted
Updated 7-Aug-17 0:52am

To call the random number between 1 and 20 just use the solution #1

But... to make it repetition safe...

0) Create an empty result_array for your 10 result numbers
1) Fill one check_array[20] with numbers from 1 to 20 to track what has been used

2) within a loop until you get the 10 random numbers without repetition
   2.1) call the random function of solution #1
   2.2) check if the number has been already used (is this number still in the array of (1)?)
      2.2.1) If yes... save the random number to the next empty position of your results array and put a "0" in that check_array's position
      2.2.2) if not... start 2 again

   2.3) if you already have all 10 non-repeated numbers... end loop

3) Print out your results
 
Share this answer
 
v4
A direct solution is, as already suggested by Grame_Grant, using the power of the C++ standard library: random_shuffle - C++ Reference[^].

On the other end if you want to implement yourself the algorithm, then I describe it here: Random extraction of 5 cards from a deck[^].
 
Share this answer
 
Comments
Graeme_Grant 7-Aug-17 7:26am    
5+ :)
An alternative [creative] solution is to randomly shuffle a set of numbers. This way there will be no repeats... ;)

This Google Search has many different ways of doing it: c++ random shuffle array[^]
 
Share this answer
 
Comments
CPallini 7-Aug-17 6:47am    
5.
Graeme_Grant 7-Aug-17 6:49am    
Thanks... Others have written the code, so there was no point in me doing it. Besides, there are a lot more choices with that link... ;)
Try
x = (rand() % 20) + 1;
 
Share this answer
 
There are basically 2 solutions:
1) Pick a random number as many times as you want. The downside is that you have to check against previously picked numbers to avoid repeating. This technique degenerate as you want more numbers, the worst case is if you want 20 number non repeating in a list of 20.

2) Second technique is shuffling: build a list of all 20 numbers you want.
Then pick a number in 0 to 19 and swap with first element of list.
Then pick a number in 1 to 19 and swap with second element of list. ...
It is a little more complicated, but it don't degenerate. This is the technique of choice to shuffle a deck of cards.
 
Share this answer
 
v2
Comments
Nelek 7-Aug-17 6:11am    
I do agree with suffle as more efficient, but for a homework is a bit too advanced ;)
Patrice T 7-Aug-17 6:31am    
Probably a matter of thinking/liking of OP.
Nelek 7-Aug-17 6:34am    
More a problem of thinking / liking of the teacher ;P
Patrice T 7-Aug-17 6:55am    
possibly :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900