Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- array = [] #DEFINES THE ARRAY
- array[0] = rand(10) #THIS CHOOSES FIRST NUMBER (FN) AND PUTS IT IN THE FIRST ARRAY SPOT
- p "fn = #{array[0]}"
- for i in 1..6 #START OF FOR LOOP TO GET 6 NUMBERS IN THE ARRAY - WHY 6? THE FIRST NUMBER THAT POPPED IN MY HEAD
- p "Iteration #{i} -------------------------" # THIS IS JUST SO I KNOW WHERE I AM IN THE LOOPS
- @x = rand(10) #THIS CHOOSES THE NEXT NUMBER AND ALL NUMBERS AFTER
- array.each do |uc| # THIS IS THE LOOP THAT COMPARES ALL NUMBERS
- @type = @x == uc ? "yes" : "no" #DOES THE CHOSEN NUMBER EQUAL ANY NUMBER IN THE ARRAY
- p "does #{uc} = #{@x}? #{@type}"
- if @type == "yes" # IF THE COMPARE IS TRUE, I DON'T WANT ANYTHING DONE. IT WILL CYCLE THRU AND GET A NEW NUMBER
- i = 1
- p "YES #{@x} = #{uc}"
- break
- end #END OF IF YES
- end #END OF ARRAY EACH
- if @type == "no" #IF NO, PUT NEXT NUMBER (@X) INTO THE NEXT ARRAY SPOT.
- p "in last if type= #{@type}" #THESE STATEMENTS JUST PRINT OUT THE DIFFERENT VARIABLES SO I KNOW I AM GETTING WHAT I EXPECT
- p "in last if x = #{@x}"
- p "in last if i = #{i}"
- @x = array[i] #THIS "SHOULD" FILL THE NEXT ARRAY SPOT - BUT DOESN'T SEEM TO
- p "#{array[i]} is in the array" #THIS PRINT OUT IS BLANK - STATEMENT ABOVE DID NOT WORK.
- p array[i]
- end #END OF IF NO
- end #END OF FOR LOOP
- p array #PRINTS OUT THE CONTENTS OF THE ARRAY
- (1..10).to_a.shuffle
- #=> [4, 10, 1, 7, 3, 5, 8, 2, 9, 6]
- (1..10).to_a.sample(7) #=> [2, 9, 1, 6, 8, 10, 4]
- array = []
- array[0] = rand 10
- puts "fn = #{array[0]}"
- for i in 1..6
- puts "Iteration #{i} -------------------------"
- x = rand 10
- array.each do |uc|
- $type = x == uc ? "yes" : "no"
- puts "does #{uc} = #{x}? #{$type}"
- if $type == "yes"
- puts "YES #{x} = #{uc}"
- break
- end
- end
- if $type == "no"
- puts "in last if type= #{$type}"
- puts "in last if x = #{x}"
- puts "in last if i = #{i}"
- array[i] = x
- puts "#{array[i]} is in the array"
- p array[i]
- else
- redo
- end
- end
- p array
- array = []
- array[0] = rand 10
- puts "fn = #{array[0]}"
- for i in 1..6
- puts "Iteration #{i} -------------------------"
- x = rand 10
- type = array.include?(x) ? "yes" : "no"
- puts "does array include #{x}? #{type}"
- if type == "no"
- puts "in last if type=#{type}, x=#{x}, i=#{i}"
- array[i] = x
- puts "#{array[i]} is in the array"
- else
- redo
- end
- end
- p array
- array = []
- array[0] = rand 10
- puts "fn = #{array[0]}"
- for i in 1..6
- puts "Iteration #{i} -------------------------"
- x = rand 10
- if (type = array.include? x).tap{ puts "does array include #{x}? #{type : "yes" : "no"}" }
- redo
- else
- puts "in last if type=#{type}, x=#{x}, i=#{i}"
- array[i] = x
- puts "#{array[i]} is in the array"
- end
- end
- p array
- array = []
- 7.times do |i|
- puts "Iteration #{i} -------------------------"
- x = rand 10
- redo if array.include?(x).tap{ |type| puts "does array include #{x}? #{type ? "yes" : "no"}" }
- puts "x=#{x}, i=#{i}"
- array[i] = x
- puts "#{array[i]} is in the array"
- end
- p array
- a = []
- while a.length < 7
- unless a.include? random_number = rand(10)
- a << random_number
- end
- end
- p a
Add Comment
Please, Sign In to add comment