You can: create an array of 5 elements (either statically or on the fly )
run a loop which will generate random number
search whether that random number is already in the array
as long as the number is one present in the array, keep on generating random numbers.
If no match is found, insert that number into the array.
Keep doing this till all the array slots are filled with random numbers.
Though the method is a bit ad hoc ( searching the entire array each time a random number is generated is bit too much) but should suffice your purpose.
Also why is your random generation method so complicated... Why not do something simple as:
random_number = min + rand( ) % max ; This will generate random numbers between minimum and maximum, both inclusive.
Also for permutation purpose, you can try here ... Though the algorithm is for permutation of character arrays, you can easily adapt it for integer ones.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Another confusing request...
I'm new at C++ and I'm trying to right a program that will generate random numbers from a range of 1-19. Here's how it should work, the program will generate 5 random numbers all ranged 1-19 and in random order. Once it chooses the first number, that number used for position 1(first number) cannot be used again, and after the second number is chosen it cannot be used again and so on. So basically it's like
postion one-19 possibilities
position two -18 possibilities (1-19 excluding position one number)
and so on until we get to position 5.This claims you want random numbers, but this:What I need is for the program to print out all possible combinations of these numbers.
claims you want all permutations. You can't do both. If you generate random numbers you can never be guaranteed to generateall permutations randomly. I see 2 programs here
1) generate 5 random numbers
2) generate a list of permutations.
~s.o.s~ has basically given you the help you need for each question. Although I have to say that for readability
random_number = min + rand( ) % max ;
needs parentheses: random_number = min + (rand( ) % max);
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
> where each column vector is normally distributed, between -1 and +1, and the row vectors are uniformly distributed between -1 and +1
this is mathematically impossible. if row vectors are uniformly distributed between -1 and +1, any element in any row (read any element in your 2d array) is equally likely to have any value in the range (-1,+1) and therefore the column vectors will also be uniformly distributed. standard normal distribution is the normal distribution with a mean of zero and a variance of one; about 99.7% of the sample points would lie in the range (-3,+3), only about 68% would lie in the range (-1,+1). the normal distribution is a *continuous* distribution, theoretically in the range(-infinity,+infinity).
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
>> If i start with 1000 uniformly distributed random numbers and then convert them into Normally ditributed numbers, and have 38 such sets, there is no guarantee that the 1000 corresponding sets of 38 numbers shall be uniformly distributed. It does not matter what range they are within.
could this work? generate 38000 uniformly distributed random numbers. take the largest 1000 of these and convert them to normally ditributed numbers, repeat for the next largest 1000 and so on till you have 38 sets. don't know enough statistics to say for sure. but it does look intuitive.
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287