Random
How do I use Random
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: frmThiThat.cs
Copy
34 void Load_DeThiThat()
35 {
36 try
37 {
38 SqlConnection cnn = new SqlConnection("server=.\\SQLEXPRESS;database=QL_Thitracnghiem;integrated security =true");
39 SqlDataAdapter da = new SqlDataAdapter("select cauhoi.mach,cauhoi,a,b,c,d ,dapan from cauhoi,DAPAN where cauhoi.mach=DAPAN.mach", cnn);
40 DataTable BangQuestion = new DataTable();
41 da.Fill(BangQuestion);
42 TaoBangRandomCauHoi(BangQuestion);
43 }
44 catch (Exception ex)
45 { MessageBox.Show(ex.Message); }
46 p = 60;//nhap thoi gian thi
47 s = 60;
48 h = 0;
49 }
File name: frmThiThat.cs
Copy
50 void TaoBangRandomCauHoi(DataTable BangQuestion)
51 {
52
53 try
54 {
55 Random Rnd = new Random();
56 ArrayList ArrQuestion = new ArrayList();
57
58 ArrQuestion.Clear();
59 int x, dem = 0;
60 int SoCauTrongBangGoc = BangQuestion.Rows.Count;
61 while (dem < SoCauNgauNhien)
62 {
63 x = Rnd.Next(0, SoCauTrongBangGoc);
64 if (!ArrQuestion.Contains(x))
65 {
66 ArrQuestion.Add(x);
67 dem++;
68 }
69 }
70 for (int j = SoCauTrongBangGoc - 1; j >= 0; j--)
71 if (!ArrQuestion.Contains(j))
72 BangQuestion.Rows.RemoveAt(j);
73
74 BangDeThi = BangQuestion;
75 BangDeThi.Columns.Add("cauhoi,DAPAN");
76
77 }
78 catch (Exception ex)
79 { MessageBox.Show(ex.Message); }
80
81 }
File name: frmThiThat.cs
Copy
82 void TaoBangRandomCauTraLoi()
83 {
84 try
85 {
86 SoCauHoi = BangDeThi.Rows.Count;
87 string DapAnDung = "";
88 string A, B, C, D;
89 int DapAn;
90 Random Rnd = new Random();
91 ArrayList ArrDapAn = new ArrayList();
92 for (int i = 0; i < SoCauHoi; i++)
93 {
94 A = "";
95 B = "";
96 C = "";
97 D = "";
98 DapAnDung = "";
99 DapAn = 0;
100 ArrDapAn.Clear();
101 ArrDapAn.Add(2);
102 ArrDapAn.Add(3);
103 ArrDapAn.Add(4);
104 ArrDapAn.Add(5);
105
106 DapAn = Rnd.Next(ArrDapAn.Count);
107 A = BangDeThi.Rows[i][(int)ArrDapAn[DapAn]].ToString();
108 if ((BangDeThi.Rows[i][6].ToString().ToUpper().Contains("A") && (int)ArrDapAn[DapAn] == 2) || (BangDeThi.Rows[i][6].ToString().ToUpper().Contains("B") && (int)ArrDapAn[DapAn] == 3) || (BangDeThi.Rows[i][6].ToString().ToUpper().Contains("C") && (int)ArrDapAn[DapAn] == 4) || (BangDeThi.Rows[i][6].ToString().ToUpper().Contains("D") && (int)ArrDapAn[DapAn] == 5))
109 DapAnDung += "A";
110 ArrDapAn.RemoveAt(DapAn);
111 DapAn = Rnd.Next(ArrDapAn.Count);
112 B = BangDeThi.Rows[i][(int)ArrDapAn[DapAn]].ToString();
113 if ((BangDeThi.Rows[i][6].ToString().ToUpper().Contains("A") && (int)ArrDapAn[DapAn] == 2) || (BangDeThi.Rows[i][6].ToString().ToUpper().Contains("B") && (int)ArrDapAn[DapAn] == 3) || (BangDeThi.Rows[i][6].ToString().ToUpper().Contains("C") && (int)ArrDapAn[DapAn] == 4) || (BangDeThi.Rows[i][6].ToString().ToUpper().Contains("D") && (int)ArrDapAn[DapAn] == 5))
114 DapAnDung += "B";
115 ArrDapAn.RemoveAt(DapAn);
116 DapAn = Rnd.Next(ArrDapAn.Count);
117 C = BangDeThi.Rows[i][(int)ArrDapAn[DapAn]].ToString();
118 if ((BangDeThi.Rows[i][6].ToString().ToUpper().Contains("A") && (int)ArrDapAn[DapAn] == 2) || (BangDeThi.Rows[i][6].ToString().ToUpper().Contains("B") && (int)ArrDapAn[DapAn] == 3) || (BangDeThi.Rows[i][6].ToString().ToUpper().Contains("C") && (int)ArrDapAn[DapAn] == 4) || (BangDeThi.Rows[i][6].ToString().ToUpper().Contains("D") && (int)ArrDapAn[DapAn] == 5))
119 DapAnDung += "C";
120 ArrDapAn.RemoveAt(DapAn);
121 DapAn = Rnd.Next(ArrDapAn.Count);
122 D = BangDeThi.Rows[i][(int)ArrDapAn[DapAn]].ToString();
123 if ((BangDeThi.Rows[i][6].ToString().ToUpper().Contains("A") && (int)ArrDapAn[DapAn] == 2) || (BangDeThi.Rows[i][6].ToString().ToUpper().Contains("B") && (int)ArrDapAn[DapAn] == 3) || (BangDeThi.Rows[i][6].ToString().ToUpper().Contains("C") && (int)ArrDapAn[DapAn] == 4) || (BangDeThi.Rows[i][6].ToString().ToUpper().Contains("D") && (int)ArrDapAn[DapAn] == 5))
124 DapAnDung += "D";
125 BangDeThi.Rows[i][2] = A;
126 BangDeThi.Rows[i][3] = B;
127 BangDeThi.Rows[i][4] = C;
128 BangDeThi.Rows[i][5] = D;
129 BangDeThi.Rows[i][6] = DapAnDung;
130 }
131 }
132 catch (Exception ex)
133 { MessageBox.Show(ex.Message); }
134 }
File name: frmThiThat.cs
Copy
190 private void frmThiThat_Load(object sender, EventArgs e)
191 {
192
193
194 lbNgaythang.Text = DateTime.Now.Day.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + DateTime.Now.Year.ToString();
195
196 {
197 try
198 {
199 if (DeThi == true)
200 Load_DeThiThat();
201 else
202 Load_DeThiThat();
203 TaoBangRandomCauTraLoi();
204 LoadCauHoiVaoControl();
205 timer1.Start();
206
207 btPre.Enabled = false;
208 }
209 catch (Exception ex)
210 { MessageBox.Show(ex.Message); }
211 }
212
213 }
File name: OnClickRequestOwnership.cs
Copy
8 public void OnClick()
9 {
10 if( Input.GetKey( KeyCode.LeftShift ) || Input.GetKey( KeyCode.RightShift ) )
11 {
12 Vector3 colVector = new Vector3( Random.Range( 0.0f, 1.0f ), Random.Range( 0.0f, 1.0f ), Random.Range( 0.0f, 1.0f ) );
13 this.photonView.RPC( "ColorRpc", PhotonTargets.AllBufferedViaServer, colVector );
14 }
15 else
16 {
17 if( this.photonView.ownerId == PhotonNetwork.player.ID )
18 {
19 Debug.Log( "Not requesting ownership. Already mine." );
20 return;
21 }
22
23 this.photonView.RequestOwnership();
24 }
25 }
File name: BallControl.cs
Copy
10 void GoBall() {
11 float rand = Random.Range (0, 2);
12 if (rand < 1) {
13 rb2d.AddForce (new Vector2 (20, -15));
14 } else {
15 rb2d.AddForce (new Vector2 (-20, -15));
16 }
17 }
File name: GUIFriendFinding.cs
Copy
10 void Start()
11 {
12 // If a user should be "findable", the client must set a playerName before connecting.
13 // This is then used during connect and the client can be found by others.
14 // Setting the playerName before connect, enables others to locate your game:
15 PhotonNetwork.playerName = "usr" + (int)Random.Range(0, 9);
16
17
18 // Photon Cloud does not implement community features for users but can work with external friends lists.
19 // We assume you get some list of IDs of your friends.
20 friendListOfSomeCommunity = FetchFriendsFromCommunity();
21
22
23 GuiRect = new Rect(Screen.width / 4, 80, Screen.width / 2, Screen.height - 100);
24 }
File name: GUIFriendFinding.cs
Copy
52 public void OnGUI()
53 {
54 if (!PhotonNetwork.insideLobby)
55 {
56 // this feature is only available on the Master Client. Check either: insideLobby or
57 // PhotonNetwork.connectionStateDetailed == PeerState.Authenticated or
58 // PhotonNetwork.connectionStateDetailed == PeerState.JoinedLobby
59
60 // for simplicity (and cause we know we WILL join the lobby, we can just check that)
61 return;
62 }
63
64
65 GUILayout.BeginArea(GuiRect);
66
67 GUILayout.Label("Your (random) name: " + PhotonNetwork.playerName);
68 GUILayout.Label("Your friends: " + string.Join(", ",this.friendListOfSomeCommunity));
69
70
71 GUILayout.BeginHorizontal();
72 if (GUILayout.Button("Find Friends"))
73 {
74 PhotonNetwork.FindFriends(this.friendListOfSomeCommunity);
75 }
76 if (GUILayout.Button("Create Room"))
77 {
78 PhotonNetwork.CreateRoom(null); // just a random room
79 }
80 GUILayout.EndHorizontal();
81
82
83 if (PhotonNetwork.Friends != null)
84 {
85 foreach (FriendInfo info in PhotonNetwork.Friends)
86 {
87 GUILayout.BeginHorizontal();
88 GUILayout.Label(info.ToString());
89 if (info.IsInRoom)
90 {
91 if (GUILayout.Button("join"))
92 {
93 PhotonNetwork.JoinRoom(info.Room);
94 }
95 }
96 GUILayout.EndHorizontal();
97 }
98 }
99
100 GUILayout.EndArea();
101 }
File name: GUIFriendsInRoom.cs
Copy
15 public void OnGUI()
16 {
17 if (PhotonNetwork.connectionStateDetailed != PeerState.Joined)
18 {
19 return;
20 }
21
22 GUILayout.BeginArea(GuiRect);
23
24 GUILayout.Label("In-Game");
25 GUILayout.Label("For simplicity, this demo just shows the players in this room. The list will expand when more join.");
26 GUILayout.Label("Your (random) name: " + PhotonNetwork.playerName);
27 GUILayout.Label(PhotonNetwork.playerList.Length + " players in this room.");
28 GUILayout.Label("The others are:");
29 foreach (PhotonPlayer player in PhotonNetwork.otherPlayers)
30 {
31 GUILayout.Label(player.ToString());
32 }
33
34 if (GUILayout.Button("Leave"))
35 {
36 PhotonNetwork.LeaveRoom();
37 }
38 GUILayout.EndArea();
39 }
File name: HubGui.cs
Copy
28 void OnGUI()
29 {
30 GUI.skin = this.Skin;
31 GUILayout.Space(10);
32
33 GUILayout.BeginHorizontal();
34 GUILayout.Space(10);
35 scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.Width(320));
36
37 GUILayout.Label("Basics", m_Headline);
38 if (GUILayout.Button("Demo Boxes", GUILayout.Width(280)))
39 {
40 demoDescription = "
41 demoBtn = new DemoBtn() { Text = "Start", Link = "DemoBoxes-Scene" };
42 }
43 if (GUILayout.Button("Demo Worker", GUILayout.Width(280)))
44 {
45 demoDescription = "
46 demoBtn = new DemoBtn() { Text = "Start", Link = "DemoWorker-Scene" };
47 }
48 if (GUILayout.Button("Movement Smoothing", GUILayout.Width(280)))
49 {
50 demoDescription = "
51 demoBtn = new DemoBtn() { Text = "Start", Link = "DemoSynchronization-Scene" };
52 }
53
54 GUILayout.Label("Advanced", m_Headline);
55 if (GUILayout.Button("Ownership Transfer", GUILayout.Width(280)))
56 {
57 demoDescription = "
58 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoChangeOwner-Scene" };
59 this.webLink = new DemoBtn();
60 }
61 if (GUILayout.Button("Pickup, Teams, Scores", GUILayout.Width(280)))
62 {
63 demoDescription = "
64 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoPickup-Scene" };
65 this.webLink = new DemoBtn();
66 }
67
68 GUILayout.Label("Feature Demos", m_Headline);
69 if (GUILayout.Button("Chat", GUILayout.Width(280)))
70 {
71 demoDescription = "
72 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoChat-Scene" };
73 this.webLink = new DemoBtn();
74 }
75 if (GUILayout.Button("RPG Movement", GUILayout.Width(280)))
76 {
77 demoDescription = "
78 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoRPGMovement-Scene" };
79 this.webLink = new DemoBtn();
80 }
81 if (GUILayout.Button("Mecanim Animations", GUILayout.Width(280)))
82 {
83 demoDescription = "
84 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoMecanim-Scene" };
85 this.webLink = new DemoBtn();
86 }
87 if (GUILayout.Button("2D Game", GUILayout.Width(280)))
88 {
89 demoDescription = "
90 this.demoBtn = new DemoBtn() { Text = "Start", Link = "Demo2DJumpAndRunWithPhysics-Scene" };
91 this.webLink = new DemoBtn();
92 }
93 if (GUILayout.Button("Friends & Authentication", GUILayout.Width(280)))
94 {
95 demoDescription = "
96 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoFriends-Scene" };
97 this.webLink = new DemoBtn();
98 }
99
100 GUILayout.Label("Tutorial", m_Headline);
101 if (GUILayout.Button("Marco Polo Tutorial", GUILayout.Width(280)))
102 {
103 demoDescription = "
104 this.demoBtn = new DemoBtn() { Text = "Start", Link = "MarcoPolo-Scene" };
105 this.webLink = new DemoBtn() { Text = "Open Tutorial (www)", Link = "http://tinyurl.com/nmylf44" };
106 }
107 GUILayout.EndScrollView();
108
109 GUILayout.BeginVertical(GUILayout.Width(Screen.width - 345));
110 GUILayout.Label(demoDescription);
111 GUILayout.Space(10);
112 if (!string.IsNullOrEmpty(this.demoBtn.Text))
113 {
114 if (GUILayout.Button(this.demoBtn.Text))
115 {
116 Application.LoadLevel(this.demoBtn.Link);
117 }
118 }
119 if (!string.IsNullOrEmpty(this.webLink.Text))
120 {
121 if (GUILayout.Button(this.webLink.Text))
122 {
123 Application.OpenURL(this.webLink.Link);
124 }
125 }
126 GUILayout.EndVertical();
127
128
129 GUILayout.EndHorizontal();
130 }
Download file with original file name:Random
Random 129 lượt xem
Gõ tìm kiếm nhanh...