randomArray.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package main
  2. import (
  3. "crypto/rand"
  4. "fmt"
  5. "math/big"
  6. )
  7. func notmain() {
  8. //understanding pointers...
  9. var i int = 1
  10. var iArray [1]int
  11. p := &i
  12. iArray[0] = *p
  13. i = 2
  14. fmt.Println(iArray[0])
  15. fmt.Println(i)
  16. //understanding sclices...
  17. var arr [5]int
  18. arr[0] = 0
  19. arr[1] = 1
  20. arr[2] = 2
  21. arr[3] = 3
  22. arr[4] = 4
  23. fmt.Println(arr[:1])
  24. fmt.Println(arr[1:3]) // = [lower:upper)
  25. //later this will be taken from application, this is only for testing
  26. var topicsOfInterest []int
  27. var lenTopicList = 100
  28. //creates fake topicOfInterest if client has no interests
  29. //creates random length with max being len(topicList)
  30. if len(topicsOfInterest) == 0 {
  31. maxLength := lenTopicList / 2
  32. if maxLength == 0 {
  33. value, err := rand.Int(rand.Reader, big.NewInt(int64(lenTopicList+1)))
  34. if err != nil {
  35. panic(err)
  36. }
  37. maxLength = int(value.Int64())
  38. }
  39. fmt.Println("MaxLength: ", maxLength)
  40. lengthBig, err := rand.Int(rand.Reader, big.NewInt(int64(maxLength+1)))
  41. if err != nil {
  42. panic(err)
  43. }
  44. length := int(lengthBig.Int64())
  45. fmt.Println("Actual length: ", length)
  46. if length == 0 {
  47. length++
  48. }
  49. fakeTopicsOfInterest := make([]int, length)
  50. //fills the array with unique random ascending values in range with len(topicList)
  51. for index := 0; index < length; index++ {
  52. min := (index * (lenTopicList / length)) + 1
  53. max := ((index + 1) * (lenTopicList / length))
  54. if max == lenTopicList-1 {
  55. max++
  56. }
  57. bigNumber, err := rand.Int(rand.Reader, big.NewInt(int64(max-min+1)))
  58. if err != nil {
  59. panic(err)
  60. }
  61. var number int = int(bigNumber.Int64()) + min
  62. fakeTopicsOfInterest[index] = number
  63. fmt.Println("Min: ", min, " Max: ", max, " Value chosen: ", fakeTopicsOfInterest[index])
  64. }
  65. }
  66. }