473,320 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Same Random result ?!

dear friends .


I want to create a instance of [R] class in [foreach] statement.
but all 10 responses are same same!



//
Expand|Select|Wrap|Line Numbers
  1. using System;
  2.  
  3. public partial class Class1: System.Web.UI.Page
  4. {
  5. protected void Page_Load(object sender, EventArgs e)
  6. {
  7. string[] k = new string[10];
  8. R cl;
  9. foreach(string kx in k)
  10. {
  11. cl = new R();
  12. Response.Write(cl.Ra() + "<br>");
  13. }
  14. }
  15. }
  16.  
  17. public class R
  18. {
  19. private Random r;
  20. public R()
  21. {
  22. r = new Random();
  23. }
  24. public int Ra()
  25. {
  26. return r.Next(100);
  27. }
  28. }
  29.  
May 20 '08 #1
3 998
Plater
7,872 Expert 4TB
Its because you do it so fast.
The random seed comes from the timer object, all of those calls happen before it changes.

Expand|Select|Wrap|Line Numbers
  1. foreach (string kx in k)
  2. {
  3.    cl = new R();
  4.    Console.WriteLine(cl.Ra());//i was in a windows app so I changed it to this
  5.    System.Threading.Thread.Sleep(100);
  6. }
  7.  
That gave me different numbers each time.

I would recomend you maybe re-think your class design since Thread.Sleep() is probably not good in a webpage context.

Perhaps like this:
Expand|Select|Wrap|Line Numbers
  1. public class R
  2. {
  3.    private static Random r=new Random();
  4.    public R()
  5.    {
  6.       //r = new Random();
  7.    }
  8.    public int Ra()
  9.    {
  10.       return r.Next(100);
  11.    }
  12. }
  13.  
May 20 '08 #2
Curtis Rutland
3,256 Expert 2GB
Unless there is more that you will be doing with this class, why do you need the class in the first place?

The same results can be achieved much simpler:
Expand|Select|Wrap|Line Numbers
  1. public partial class _Default : System.Web.UI.Page
  2. {
  3.     protected void Page_Load(object sender, EventArgs e)
  4.     {
  5.         string[] k = new string[10];
  6.         Random cl = new Random();
  7.         foreach (string kx in k)
  8.             Response.Write(cl.Next(100) + "<br>");
  9.     }
  10. }
  11.  
Its because you do it so fast.
The random seed comes from the timer object, all of those calls happen before it changes.

Expand|Select|Wrap|Line Numbers
  1. foreach (string kx in k)
  2. {
  3.    cl = new R();
  4.    Console.WriteLine(cl.Ra());//i was in a windows app so I changed it to this
  5.    System.Threading.Thread.Sleep(100);
  6. }
  7.  
That gave me different numbers each time.

I would recomend you maybe re-think your class design since Thread.Sleep() is probably not good in a webpage context.

Perhaps like this:
Expand|Select|Wrap|Line Numbers
  1. public class R
  2. {
  3.    private static Random r=new Random();
  4.    public R()
  5.    {
  6.       //r = new Random();
  7.    }
  8.    public int Ra()
  9.    {
  10.       return r.Next(100);
  11.    }
  12. }
  13.  
May 20 '08 #3
I would recomend you maybe re-think your class design since Thread.Sleep() is probably not good in a webpage context.

thank you very very much.
May 20 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

70
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like...
8
by: Jack | last post by:
When I use rand(), is the RAND_MAX value how long I am guaranteed that the same value will not appear twice? And is this a floating window? For example, if RAND_MAX is 32767, and I make...
11
by: Mark Rae | last post by:
Hi, My R&D department has asked me to look at threading in a Web Service written in C#, so I came up with the following code: using System; using System.ComponentModel; using...
46
by: Your Uncle | last post by:
About a month ago, Heathfield posted the peudosource for random permuting from TAOCP. It was all of maybe five lines. You needed to be able to do two things: be able to get a random number in a...
12
by: Pascal | last post by:
hello and soory for my english here is the query :"how to split a string in a random way" I try my first shot in vb 2005 express and would like to split a number in several pieces in a random way...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I generate a random integer from 1 to N?...
3
by: td0g03 | last post by:
Like the titles says I'm suppose to generate a random number then divide that by a number inputed by a user. The random number can range from 2-8. I tried to do the code, but I get some weird result...
3
by: tshad | last post by:
I have a page that I am getting a username and password as a random number (2 letters, one number and 4 more letters) I have 2 functions I call: *************************************************...
3
by: Army1987 | last post by:
Is there anything wrong with this program? It seems to behave strangely if I give stdin EOF when asked for the character set... /* BEGIN pwdgen.c */ #include <stdio.h> #include "random.h"...
24
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.