Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I scramble different ranges of numbers?

Status
Not open for further replies.

InsideEdge

Instructor
Apr 5, 2005
35
US
Hi, I’m trying to produce random numbers. Not just random numbers, I would like to produce a group of 4 random number numbers each in a different range. Like 1 to 4, 5 to 8, 9 to 12. I’d like for each range to be scrambled in a random order. I’ve looked all over the internet for samples but could not find anything.

My goal is to write a program that pulls questions from a file or a database in groups of 4’s. Each time I pull from the file or database, I want them to be pulled in a different or random order.

Please help.


 
Rather than just ask if we can provide a script, why don't you post what have you tried so far? We can then help you and point out what may be wrong then you will be able to come up with your own solution.

You'll also have to post what you mean by a range (e.g. would it have to be returned in an array, collection etc).


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
InsideEdge,

My advice is to write down the algorithm in english. Then translate it to your language of choice. This should help you understand the coding required.

I'll also suggest making a bunch of classes to help with your task. I know it seems like overkill, but in the end it will be easier. Perhaps Question, QuestionGroup (holds fixed number of questions), QuestionPool (holds variable number of questions, has Add and Remove methods), etc.
 
Can you elaborate?

Do you want a single sequence of numbers, where each set of 4 is randomized internally, like so:

4 1 3 2 8 5 6 7 12 9 10 11

Or iteratively in a loop, ascending, like so:

4 1 3 2

8 5 6 7

12 9 10 11

or in a loop, but randomized, like so:

8 5 6 7

12 9 10 11

4 1 3 2



 
The last two examples would suit me fine. Here's the sample that I've been trying to work with. I got it from the internet and tried to tweek it but I can't think of a way to minipulate the range. I would create multiple classes but I don't know how to get a range from the stack like from 5 to 8 or so. Here's the code:



namespace RandomN
{
using System;
using System.Collections;
/// <summary>
/// Summary description for Class1.
/// </summary>
public class RandomN
{
public static int Numbers, EndRange;
//Displays titles and gets start values
public static void DisplayTitles()
{
EndRange=4;
Numbers=4;

}
//Generate our unique numbers and push them onto stack
public static void GenerateNumbers(Stack s, Dice d)
{
bool exists=true;
int i;
int val;

for (i=1;i<=Numbers;i++)
{
val=d.RollDice(EndRange); //Roll EM!
exists=s.Contains(val);
while (exists==true) //Reroll if we already
{ //have that number
val=d.RollDice(EndRange);
exists=s.Contains(val);
}
s.Push(val);
}
}

//Retrieve all values from the stack and output to console
public static void DisplayResults(Stack s)
{
for (int i=1;i<=Numbers;i++)
{
System.Console.WriteLine(s.Pop());
}
}

//Entry point for program
public static int Main (string[] args)
{
Stack s = new Stack();
Dice d = new Dice();
DisplayTitles();
GenerateNumbers(s,d);
DisplayResults(s);
return 0;
}
}

//Class to simulate a dice
public class Dice
{
public int RollDice(int sides)
{
Random r = new Random();
int i=0;
while(i==0)
{
i=r.Next(sides+1);//Get another random number but
} //discard zero values
return i;
}
}
}
 
I would create a parallel array of random numbers, an array of number from 0 to n-1, sort them with Array.Sort(random, zeroN) then use the sorted zero to n-1 array as an indices into the original array(s).

- free online Compare/Diff of snippets
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top