Fortran [Fortran] How to loss different random number

AI Thread Summary
To ensure that all generated random numbers in a lottery program are unique, the discussion suggests modifying the existing code. The user initially faced an issue where the same random number was generated due to a fixed seed. The proposed solution involves creating an array called "already_generated" to track which numbers have already been selected. Each time a new random number is generated, the program checks this array to confirm that the number hasn't been previously chosen. If it has, the program continues to generate new numbers until a unique one is found. This approach effectively guarantees that all six random integers will be different.
Galizius
Messages
14
Reaction score
0
Hello I am wondering how can I make sure that every number which I am losing will be different? I am using the following code:

Code:
program lottery
implicit none
real(kind=4) :: x
real(kind=8) :: y
integer :: i, seed= 64256

call srand(seed)

do i=1,6
  x=rand()
  ! to make the lossing interval [1,49]
  y=ceiling(dble(x)*49.0d0)
  write(*,*) y
enddo
end program lottery

I have set this seed for a purpose, because it gives two same values of the y. I would like to know how to carry out with this problem.
 
Technology news on Phys.org
If you need all of the 6 random integers to be different, try this:
make an array, already_generated, of 49 zeros,
for a new random integer, set the corresponding index of already_generated to 1,
loop on random integers till a new one is generated (check the corresponding index of already_generated)
 
Thank You so much it works :)
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...

Similar threads

Replies
3
Views
9K
Replies
8
Views
2K
Replies
6
Views
3K
Replies
3
Views
3K
Replies
1
Views
3K
Replies
22
Views
5K
Replies
21
Views
3K
Replies
59
Views
11K
Back
Top