Thank you for being a valued part of the CNET community. As of December 1, 2020, the forums are in read-only format. In early 2021, CNET Forums will no longer be available. We are grateful for the participation and advice you have provided to one another over the years.

Thanks,

CNET Support

General discussion

random number

Apr 5, 2007 4:59AM PDT

Instead of generating the number as

r.Next(2);

is it better to generate more numbers and grab one(0 or 1) from the buffer?

r.Next(99999);

Discussion is locked

- Collapse -
What...
Apr 5, 2007 5:56AM PDT

First you should name what language are you using and environment as I don't know if that next is from Java or some other language. If it is from Java then you should be aware if you use more than 32 you may run into issues since some documentation defines the function as:
"The general contract of next is that it returns an int value and if the argument bits is between 1 and 32 (inclusive), then that many low-order bits of the returned value will be (approximately) independently chosen bit values, each of which is (approximately) equally likely to be 0 or 1."

Which states nothing about if you feed it an integer greater than 32.

- Collapse -
Tomes about random.
Apr 5, 2007 7:01AM PDT

There are tomes about generating random numbers. Without the context of what the number is used for, your method is as good as another.

Bob

- Collapse -
this is lame
Apr 5, 2007 2:03PM PDT

im getting the same number every generation lmmffao! there is something majorly wrong. and yes ive tried those "Tomes" of random junk...

- Collapse -
You may have missed this in the tomes.
Apr 6, 2007 2:48AM PDT

That number 2 in your call is the seed number. Some random number use requires you run your program in a test mode which will produce the same answer each time. That's more a topic for SOFTWARE TESTING but as you can tell that's your choice to do that.

Depending on the language and library you should get the same random number sequence if you pass in the seed.

Bob

- Collapse -
slippery fish
Apr 6, 2007 4:28AM PDT

I had it all working but as soon as more randoms are generated, it becomes linear generation. and i tried calling it out of a function with multiple seeds...

- Collapse -
so blind
Apr 6, 2007 5:07AM PDT

It was all under my eyes but i couldnt see it lmmffao! i was missing the integer pass...

- Collapse -
The second pair of eyes always sees something.
Apr 6, 2007 5:33AM PDT

It's a recurring lesson. Second opinions pay off in every field except maybe bomb disposal.

Bob

- Collapse -
Random Numbers
Apr 20, 2007 3:00PM PDT

I am pretty certain that you need to seed the random function with soemthing other than the same integer to obtain a non linear string of numbers. The best way to do it is with some sort of number that will change over time. A value like the current time in milliseconds would work.