Fortran [Fortran] How to loss different random number

Click For 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 :)
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 3 ·
Replies
3
Views
9K
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
22
Views
5K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 22 ·
Replies
22
Views
5K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 59 ·
2
Replies
59
Views
11K