-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_array.py
37 lines (31 loc) · 939 Bytes
/
random_array.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# -*- coding: utf-8 -*-
"""
Created on Fri Dec 14 14:11:30 2018
@author: dingchen
给定一个随机生成器,把一个数组顺序打乱
"""
import numpy as np
import random
def search_position(num, search_list):
'''
give a number, return the position of the search_list
'''
for i,j in enumerate(search_list):
if j == num:
return i
print ('This number is not in the list of %s' %(search_list))
raise AssertionError()
def random_array(array):
random_list = []
for i in range(len(array)):
random_list.append(random.random())
random_sort = sorted(random_list)
result = np.zeros_like(array)
for i,j in enumerate(random_list):
result[search_position(j, random_sort)] = array[i]
return result
#%%
#for test
a = np.array([1,2,3,4,5])
b = random_array(a)
print ('The original is %s, and the result is %s' %(a,b))